


*************************
*POLITICAL INCORPORATION*
*************************




cd "/Users/sergi/Library/CloudStorage/Dropbox/Work/Coconut paper/ESS data/"

global pathfig "/Users/sergi/Library/CloudStorage/Dropbox/Aplicaciones/Overleaf/Migrant political integration/"



*Add current year identifier in QoG data to match with ESS data later

use qog_std_ts_jan20.dta, clear

capture drop essround
gen essround=.
replace essround=1 if year==2002
replace essround=2 if year==2004
replace essround=3 if year==2006
replace essround=4 if year==2008
replace essround=5 if year==2010
replace essround=6 if year==2012
replace essround=7 if year==2014
replace essround=8 if year==2016

capture drop expenditure_current gdp_current unempl_current

gen expenditure_current=oecd_socexpnd_t1a
gen gdp_current=wdi_gdppppcon2011
gen unempl_current=wdi_unempilo

save qog_std_ts_jan20.dta, replace


*Reshape EPL data from OECD to merge with ESS later (strictness of employment protection - individual and collective dismissals (regular contracts)

import delimited using EPL.csv, clear

sort country time 
keep if series=="EPRC_V1"


capture drop essround
gen essround=.
replace essround=1 if time==2002
replace essround=2 if time==2004
replace essround=3 if time==2006
replace essround=4 if time==2008
replace essround=5 if time==2010
replace essround=6 if time==2012
replace essround=7 if time==2014
replace essround=8 if time==2016



rename country country_name

gen country=.
replace country=1 if v2=="Austria"
replace country=2 if v2=="Belgium"
replace country=4 if v2=="Switzerland"
replace country=6 if v2=="Czech Republic"
replace country=7 if v2=="Germany"
replace country=8 if v2=="Denmark"
replace country=9 if v2=="Estonia"
replace country=10 if v2=="Spain"
replace country=11 if v2=="Finland"
replace country=12 if v2=="France"
replace country=13 if v2=="United Kingdom"
replace country=14 if v2=="Greece"
replace country=16 if v2=="Hungary"
replace country=17 if v2=="Ireland"
replace country=19 if v2=="Iceland"
replace country=20 if v2=="Italy"
replace country=22 if v2=="Luxembourg"
replace country=23 if v2=="Netherlands"
replace country=24 if v2=="Norway"
replace country=25 if v2=="Poland"
replace country=26 if v2=="Portugal"
replace country=28 if v2=="Sweden"
replace country=30 if v2=="Slovak Republic"
replace country=31 if v2=="Turkey"

rename value epl

keep country essround epl


save EPL_OECD.dta, replace

*Use European Social Survey

use ESS1-8e01.dta, clear

encode cntry, gen(country)

numlabel country, add

tab country

* We remove Bulgaria (no data on IMPIC), Cyprus (no data on IMPIC), Croatia (no data on IMPIC), Israel (different immigration history), ///
/// Lithuania (no data on IMPIC), Russia (no data on IMPIC), Slovenia (no data on IMPIC), Ukraine (no data on IMPIC).

drop if country==3 | country==5 | country==15 | country==18 | country==21 | country==27 | country==29 | country==32

*We prepare the ESS data to match immigration policy upon year of arrival

recode country 1=1 2=3 4=7 6=9 7=10 8=11 9=13 10=15 11=16 12=17 13=18 14=19 16=20 17=21 19=23 ///
20=24 22=27 23=29 24=30 25=32 26=33 28=34 30=35 31=36, gen(cntryID)


gen ccodecow=.
replace ccodecow=305 if country==1
replace ccodecow=211 if country==2
replace ccodecow=225 if country==4
replace ccodecow=316 if country==6
replace ccodecow=255 if country==7
replace ccodecow=390 if country==8
replace ccodecow=366 if country==9
replace ccodecow=230 if country==10
replace ccodecow=375 if country==11
replace ccodecow=220 if country==12
replace ccodecow=200 if country==13
replace ccodecow=350 if country==14
replace ccodecow=310 if country==16
replace ccodecow=205 if country==17
replace ccodecow=395 if country==19
replace ccodecow=325 if country==20
replace ccodecow=212 if country==22
replace ccodecow=210 if country==23
replace ccodecow=385 if country==24
replace ccodecow=290 if country==25
replace ccodecow=235 if country==26
replace ccodecow=380 if country==28
replace ccodecow=317 if country==30
replace ccodecow=640 if country==31


rename livecnta year /*rounds 5-8*/

gen birth17=yrbrn+17

replace year=birth17 if brncntr==1


/*
gen year_prov=inwyr-1 if livecntr==1 /*round 1-2*/
replace year_prov=inwyr-5 if livecntr==2
replace year_prov=inwyr-10 if livecntr==3
replace year_prov=inwyr-20 if livecntr==4
replace year=year_prov if essround==1
replace year=year_prov if essround==2

replace year_prov=inwyys-1 if livecntr==1 & essround==3 /*round 3-4*/
replace year_prov=inwyys-5 if livecntr==2  & essround==3 
replace year_prov=inwyys-10 if livecntr==3  & essround==3 
replace year_prov=inwyys-20 if livecntr==4  & essround==3 
replace year_prov=inwyys-1 if livecntr==1 & essround==4 /*round 3-4*/
replace year_prov=inwyys-5 if livecntr==2  & essround==4
replace year_prov=inwyys-10 if livecntr==3  & essround==4
replace year_prov=inwyys-20 if livecntr==4  &  essround==4
replace year=year_prov if essround==3
replace year=year_prov if essround==4
*/



merge m:m cntryID year using impic2016.dta

*keep if brncntr==2

save ESS_IMPIC_migrants.dta, replace

capture drop _merge
merge m:m ccodecow year using qog_std_ts_jan20.dta,  keepusing(oecd_socexpnd_t1a wdi_gdppppcon2011 wdi_unempilo wdi_chexppgdp wdi_expedu)


capture drop _merge
merge m:m ccodecow essround using qog_std_ts_jan20.dta,  keepusing(expenditure_current gdp_current unempl_current vi_wcoord vi_udr)



rename oecd_socexpnd_t1a expenditure
rename wdi_gdppppcon2011 gdp
rename wdi_unempilo unemp_rate
rename wdi_chexppgdp health_expend
rename wdi_expedu education_expend

/*
oecd_s~d_t1a /*public social expenditure*/
wdi_gdpppp~2011 /*GDP, PPP (constant 2011 international $)*/
wdi_unempilo /*unemployment*/
*/


save ESS_IMPIC_migrants.dta, replace


*Merge EPL data from OECD


capture drop _merge
merge m:m country essround using EPL_OECD.dta,  keepusing(epl)

save ESS_IMPIC_migrants.dta, replace


*Recoding variables: economic integration

gen income=hinctnt
replace income=hinctnta if essround>3 /*income*/

gen income_log=log(income)

tab uemp3m
gen unemp_3m =.
replace unemp_3m =1 if uemp3m==1
replace unemp_3m =0 if uemp3m==2 /*ever unemployed for more than 3 months*/

gen permanent=.
replace permanent=1 if wrkctr==1 & essround==1
replace permanent=0 if wrkctr==2 & essround==1
replace permanent=1 if wrkctra==1 & essround==1 & country==16
replace permanent=0 if wrkctra>1 & essround==1 & country==16
replace permanent=1 if wrkctra==1 & essround>1 & country!=16
replace permanent=0 if wrkctra>1 & essround>1 & country!=16 /*temporary contract*/

rename uempla unemp_now /*unemployed now*/

gen active=mnactic==1 /*active*/


*Recoding variables: political support / integration

factor trstprl trstlgl trstplc trstplt trstprt /*two factors among migrants: politicians and legal system*/
rotate, varimax


gen trust_politicians=trstprl+trstplt+trstprt
gen trust_legal=trstlgl+trstplc



tab1 stfdem /*democratic satisfaction*/

tab1 mmbprty wrkprty contplt wrkorg /*conventional participation*/
foreach x in mmbprty wrkprty contplt wrkorg {
recode `x' 2=0 1=1 
}

gen conv_part=mmbprty+wrkprty+contplt+wrkorg 

tab1 badge sgnptit pbldmn bctprd /*unconventional participation*/

foreach x in badge sgnptit pbldmn bctprd {
recode `x' 2=0 1=1 
}

gen unconv_part= badge+sgnptit+pbldmn+bctprd


gen close_party=clsprty==1

gen vote_national=vote==1


factor nwsppol rdpol tvpol /* media news consumption belong to a single factor. But unclear whether host or origin country media so let's not use this*/
rotate, varimax

gen media = nwsppol+rdpol+tvpol

*Social integration


tab ppltrst /*interpersonal trust*/

tab happy /*happiness*/

tab sclmeet /*frequency meeting with friends, relatives, colleagues*/

tab sclact /*frequency taking part in social activities*/


tab1 dscrgrp dscrrce dscrntn dscretn /* feeling disciminated*/

gen discrimination=dscretn+dscrntn+dscrrce /*discriminated by ethnic group, nationality, race*/


*Other variables

encode cntbrth, gen(cntbrth_num)
encode cntbrtha, gen(cntbrtha_num)
encode cntbrthb, gen(cntbrthb_num) 
encode cntbrthc, gen(cntbrthc_num)

tab1 cntbrthb_num cntbrthc_num /*let's do rounds 5-8 only*/
numlabel cntbrthb_num, add  
numlabel cntbrthc_num, add


*EU + Norway and Switzerland

gen eu=.
replace eu=0 if cntbrthb_num==1 /*USSR*/
replace eu=0 if cntbrthb_num==2 /*Czechoslovakia*/
replace eu=0 if cntbrthb_num==3 /*Yugoslavia*/
replace eu=0 if cntbrthb_num==4 /*Serbia and Montenegro*/
replace eu=0 if cntbrthb_num==8 /*Afghanistan*/
replace eu=0 if cntbrthb_num==9 /*Albania*/
replace eu=0 if cntbrthb_num==10 /*Armenia*/
replace eu=0 if cntbrthb_num==11 /*Netherlands Antilles*/
replace eu=0 if cntbrthb_num==12 /*Angola*/
replace eu=0 if cntbrthb_num==13 /*Argentina*/
replace eu=1 if cntbrthb_num==14 /*Austria*/
replace eu=0 if cntbrthb_num==14 & year<1995 /*Austria before accession*/
replace eu=0 if cntbrthb_num==15 /*Australia*/
replace eu=0 if cntbrthb_num==16 /*Aruba*/
replace eu=0 if cntbrthb_num==17 /*Aland Islands*/
replace eu=0 if cntbrthb_num==18 /*Azerbaijan*/
replace eu=0 if cntbrthb_num==19 /*Bosnia and Herzegovina*/
replace eu=0 if cntbrthb_num==20 /*Bangladesh*/
replace eu=1 if cntbrthb_num==21 /*Belgium*/
replace eu=0 if cntbrthb_num==22 /*Burkina Faso*/
replace eu=1 if cntbrthb_num==23 /*Bulgaria*/
replace eu=0 if cntbrthb_num==23 & year<2004 /*Bulgaria before accession*/
replace eu=0 if cntbrthb_num==24 /*Burundi*/
replace eu=0 if cntbrthb_num==25 /*Benin*/
replace eu=0 if cntbrthb_num==26 /*Brunei Darussalam*/
replace eu=0 if cntbrthb_num==27 /*Bolivia*/
replace eu=0 if cntbrthb_num==28 /*Brazil*/
replace eu=0 if cntbrthb_num==29 /*Botswana*/
replace eu=0 if cntbrthb_num==30 /*Belarus*/
replace eu=0 if cntbrthb_num==31 /*Canada*/
replace eu=0 if cntbrthb_num==32 /*Democratic Republic Congo*/
replace eu=0 if cntbrthb_num==33 /*Central African Republic*/
replace eu=0 if cntbrthb_num==34 /*Congo*/
replace eu=1 if cntbrthb_num==35 /*Switzerland: not EU but we consider so due to similar arrangements*/
replace eu=0 if cntbrthb_num==36 /*Cote Ivoire*/
replace eu=0 if cntbrthb_num==37 /*Chile*/
replace eu=0 if cntbrthb_num==38 /*Cameroon*/
replace eu=0 if cntbrthb_num==39 /*China*/
replace eu=0 if cntbrthb_num==40 /*Colombia*/
replace eu=0 if cntbrthb_num==41 /*Cuba*/
replace eu=0 if cntbrthb_num==42 /*Cape Verde*/
replace eu=1 if cntbrthb_num==43 /*Cyprus*/
replace eu=0 if cntbrthb_num==43 & year<2004 /*Cyprus*/
replace eu=1 if cntbrthb_num==44 /*Czech Republic*/
replace eu=0 if cntbrthb_num==44 & year<2004 /*Czech Republic*/
replace eu=1 if cntbrthb_num==45 /*Germany*/
replace eu=1 if cntbrthb_num==46 /*Denmark*/
replace eu=0 if cntbrthb_num==46 & year<1973 /*Denmark*/
replace eu=0 if cntbrthb_num==47 /*Dominican Republic*/
replace eu=0 if cntbrthb_num==48 /*Algeria*/
replace eu=0 if cntbrthb_num==49 /*Ecuador*/
replace eu=1 if cntbrthb_num==50 /*Estonia*/
replace eu=0 if cntbrthb_num==50 & year<2004 /*Estonia*/
replace eu=0 if cntbrthb_num==51 /*Egypt*/
replace eu=0 if cntbrthb_num==52 /*Eritrea*/
replace eu=1 if cntbrthb_num==53 /*Spain*/
replace eu=0 if cntbrthb_num==53 & year<1986 /*Spain*/
replace eu=0 if cntbrthb_num==54 /*Ethiopia*/
replace eu=1 if cntbrthb_num==55 /*Finland*/
replace eu=0 if cntbrthb_num==55 & year<1995 /*Finland*/
replace eu=0 if cntbrthb_num==56 /*Faroe Islands*/
replace eu=1 if cntbrthb_num==57 /*France*/
replace eu=0 if cntbrthb_num==58 /*Gabon*/
replace eu=1 if cntbrthb_num==59 /*Great Britain*/
replace eu=0 if cntbrthb_num==59 & year < 1973/*Great Britain*/
replace eu=0 if cntbrthb_num==60 /*Georgia*/
replace eu=0 if cntbrthb_num==61 /*Ghana*/
replace eu=0 if cntbrthb_num==62 /*Greenland*/
replace eu=0 if cntbrthb_num==63 /*Gambia*/
replace eu=0 if cntbrthb_num==64 /*Guinea*/
replace eu=0 if cntbrthb_num==65 /*Guadelupe*/
replace eu=1 if cntbrthb_num==66 /*Greece*/
replace eu=0 if cntbrthb_num==66 & year<1981 /*Greece*/
replace eu=0 if cntbrthb_num==67 /*Guatemala*/
replace eu=0 if cntbrthb_num==68 /*Guinea-Bissau*/
replace eu=0 if cntbrthb_num==69 /*Guyana*/
replace eu=0 if cntbrthb_num==70 /*Hong Kong*/
replace eu=0 if cntbrthb_num==71 /*Honduras*/
replace eu=1 if cntbrthb_num==72 /*Croatia*/
replace eu=0 if cntbrthb_num==72 & year<2004 /*Croatia*/
replace eu=1 if cntbrthb_num==73 /*Hungary*/
replace eu=0 if cntbrthb_num==73 & year<2004 /*Hungary*/
replace eu=0 if cntbrthb_num==74 /*Indonesia*/
replace eu=1 if cntbrthb_num==75 /*Ireland*/
replace eu=0 if cntbrthb_num==75 & year<1973 /*Ireland*/
replace eu=0 if cntbrthb_num==76 /*Israel*/
replace eu=0 if cntbrthb_num==77 /*India*/
replace eu=0 if cntbrthb_num==78 /*Iraq*/
replace eu=0 if cntbrthb_num==79 /*Iran*/
replace eu=0 if cntbrthb_num==80 /*Iceland*/
replace eu=1 if cntbrthb_num==81 /*Italy*/
replace eu=1 if cntbrthb_num==82 /*Jersey (UK dependency)*/
replace eu=0 if cntbrthb_num==82 & year<1973 /*Jersey (UK dependency)*/
replace eu=0 if cntbrthb_num==83 /*Jamaica*/
replace eu=0 if cntbrthb_num==84 /*Jordan*/
replace eu=0 if cntbrthb_num==85 /*Japan*/
replace eu=0 if cntbrthb_num==86 /*Kenya*/
replace eu=0 if cntbrthb_num==87 /*Kyrgyzstan*/
replace eu=0 if cntbrthb_num==88 /*Cambodia*/
replace eu=0 if cntbrthb_num==89 /*Comoros*/
replace eu=0 if cntbrthb_num==90 /*Korea, Democratic People's Republic of*/
replace eu=0 if cntbrthb_num==91 /*Korea, Republic of*/
replace eu=0 if cntbrthb_num==92 /*Kuwait*/
replace eu=0 if cntbrthb_num==93 /*Kazakhstan*/
replace eu=0 if cntbrthb_num==94 /*Lao People's Democratic Republic*/
replace eu=0 if cntbrthb_num==95 /*Lebanon*/
replace eu=0 if cntbrthb_num==96 /*Saint Lucia*/
replace eu=0 if cntbrthb_num==97 /*Liechtenstein*/
replace eu=0 if cntbrthb_num==98 /*Sri Lanka*/
replace eu=0 if cntbrthb_num==99 /*Liberia*/
replace eu=1 if cntbrthb_num==100 /*Lithuania*/
replace eu=0 if cntbrthb_num==100 & year<2004 /*Lithuania*/
replace eu=1 if cntbrthb_num==101 /*Luxembourg*/
replace eu=1 if cntbrthb_num==102 /*Latvia*/
replace eu=0 if cntbrthb_num==102 & year<2004 /*Latvia*/
replace eu=0 if cntbrthb_num==103 /*Libyan Arab Jamahiriya*/
replace eu=0 if cntbrthb_num==104 /*Morocco*/
replace eu=0 if cntbrthb_num==105 /*Moldova*/
replace eu=0 if cntbrthb_num==106 /*Montenegro*/
replace eu=0 if cntbrthb_num==107 /*Madagascar*/
replace eu=0 if cntbrthb_num==108 /*Macedonia*/
replace eu=0 if cntbrthb_num==109 /*Mali*/
replace eu=0 if cntbrthb_num==110 /*Macao*/
replace eu=0 if cntbrthb_num==111 /*Martinique*/
replace eu=0 if cntbrthb_num==112 /*Montserrat*/
replace eu=1 if cntbrthb_num==113 /*Malta*/
replace eu=0 if cntbrthb_num==113 & year<2004 /*Malta*/
replace eu=0 if cntbrthb_num==114 /*Mauritius*/
replace eu=0 if cntbrthb_num==115 /*Mexico*/
replace eu=0 if cntbrthb_num==116 /*Malaysia*/
replace eu=0 if cntbrthb_num==117 /*Mozambique*/
replace eu=0 if cntbrthb_num==118 /*Namibia*/
replace eu=0 if cntbrthb_num==119 /*Nigeria*/
replace eu=0 if cntbrthb_num==120 /*Nicaragua*/
replace eu=1 if cntbrthb_num==121 /*Netherlands*/
replace eu=1 if cntbrthb_num==122 /*Norway*/
replace eu=0 if cntbrthb_num==123 /*Nepal*/
replace eu=0 if cntbrthb_num==124 /*New Zealand*/
replace eu=0 if cntbrthb_num==125 /*Panama*/
replace eu=0 if cntbrthb_num==126 /*Peru*/
replace eu=0 if cntbrthb_num==127 /*Philipines*/
replace eu=0 if cntbrthb_num==128 /*Pakistan*/
replace eu=1 if cntbrthb_num==129 /*Poland*/
replace eu=0 if cntbrthb_num==129 & year<2004 /*Poland*/
replace eu=0 if cntbrthb_num==130 /*Palestina*/
replace eu=1 if cntbrthb_num==131 /*Portugal*/
replace eu=0 if cntbrthb_num==131 & year<1986 /*Portugal*/
replace eu=0 if cntbrthb_num==132 /*Paraguay*/
replace eu=0 if cntbrthb_num==133 /*Reunion*/
replace eu=1 if cntbrthb_num==134 /*Romania*/
replace eu=0 if cntbrthb_num==134 & year<2004 /*Romania*/
replace eu=0 if cntbrthb_num==135 /*Serbia*/
replace eu=0 if cntbrthb_num==136 /*Russia*/
replace eu=0 if cntbrthb_num==137 /*Rwanda*/
replace eu=0 if cntbrthb_num==138 /*Saudi Arabia*/
replace eu=0 if cntbrthb_num==139 /*Sudan*/
replace eu=1 if cntbrthb_num==140 /*Sweden*/
replace eu=0 if cntbrthb_num==140 & year<1995 /*Sweden*/
replace eu=0 if cntbrthb_num==141 /*Singapore*/
replace eu=1 if cntbrthb_num==142 /*Slovenia*/
replace eu=0 if cntbrthb_num==142 & year<2004 /*Slovenia*/
replace eu=1 if cntbrthb_num==143 /*Slovakia*/
replace eu=0 if cntbrthb_num==143 & year<2004 /*Slovakia*/
replace eu=0 if cntbrthb_num==144 /*Sierra Leone*/
replace eu=0 if cntbrthb_num==145 /*Senegal*/
replace eu=0 if cntbrthb_num==146 /*Somalia*/
replace eu=0 if cntbrthb_num==147 /*Suriname*/
replace eu=0 if cntbrthb_num==148 /*Sao Tome and Principe*/
replace eu=0 if cntbrthb_num==149 /*El Salvador*/
replace eu=0 if cntbrthb_num==150 /*Syria*/
replace eu=0 if cntbrthb_num==151 /*Togo*/
replace eu=0 if cntbrthb_num==152 /*Thailand*/
replace eu=0 if cntbrthb_num==153 /*Tajikistan*/
replace eu=0 if cntbrthb_num==154 /*Tunisia*/
replace eu=0 if cntbrthb_num==155 /*Turkey*/
replace eu=0 if cntbrthb_num==156 /*Trinidad and Tobago*/
replace eu=0 if cntbrthb_num==157 /*Taiwan*/
replace eu=0 if cntbrthb_num==158 /*Tanzania*/
replace eu=0 if cntbrthb_num==159 /*Ukraine*/
replace eu=0 if cntbrthb_num==160 /*Uganda*/
replace eu=0 if cntbrthb_num==161 /*United States*/
replace eu=0 if cntbrthb_num==162 /*Uruguay*/
replace eu=0 if cntbrthb_num==163 /*Uzbekistan*/
replace eu=0 if cntbrthb_num==164 /*Venezuela*/
replace eu=0 if cntbrthb_num==165 /*Vietnam*/
replace eu=0 if cntbrthb_num==166 /*Yemen*/
replace eu=0 if cntbrthb_num==167 /*South Africa*/
replace eu=0 if cntbrthb_num==168 /*Zambia*/
replace eu=0 if cntbrthb_num==169 /*Zimbabwe*/

replace eu=0 if cntbrthc_num==1 /*DDR*/
replace eu=0 if cntbrthc_num==2 /*USSR*/
replace eu=0 if cntbrthc_num==3 /*Czechoslovakia*/
replace eu=0 if cntbrthc_num==4 /*Yugoslavia*/
replace eu=0 if cntbrthc_num==5 /*Serbia and Montenegro*/
replace eu=0 if cntbrthc_num==9 /*United Arab Emirates*/
replace eu=0 if cntbrthc_num==10 /*Afghanistan*/
replace eu=0 if cntbrthc_num==11 /*Albania*/
replace eu=0 if cntbrthc_num==12 /*Armenia*/
replace eu=0 if cntbrthc_num==13 /*Angola*/
replace eu=0 if cntbrthc_num==14 /*Argentina*/
replace eu=1 if cntbrthc_num==15 /*Austria*/
replace eu=0 if cntbrthc_num==15 & year<1995 /*Austria before accession*/
replace eu=0 if cntbrthc_num==16 /*Australia*/
replace eu=0 if cntbrthc_num==17 /*Aruba*/
replace eu=0 if cntbrthc_num==18 /*Azerbajan*/
replace eu=0 if cntbrthc_num==19 /*Bosnia and Herzegovina*/
replace eu=0 if cntbrthc_num==20 /*Bangladesh*/
replace eu=1 if cntbrthc_num==21 /*Belgium*/
replace eu=0 if cntbrthc_num==22 /*Burkina Faso*/
replace eu=1 if cntbrthc_num==23 /*Bulgaria*/
replace eu=0 if cntbrthc_num==23 & year<2004 /*Bulgaria*/
replace eu=0 if cntbrthc_num==24 /*Burundi*/
replace eu=0 if cntbrthc_num==25 /*Benin*/
replace eu=0 if cntbrthc_num==26 /*Brunei Darussalam*/
replace eu=0 if cntbrthc_num==27 /*Bolivia*/
replace eu=0 if cntbrthc_num==28 /*Bonaire, Sint Eustatius and Saba*/
replace eu=0 if cntbrthc_num==29 /*Brazil*/
replace eu=0 if cntbrthc_num==30 /*Belarus*/
replace eu=0 if cntbrthc_num==31 /*Canada*/
replace eu=0 if cntbrthc_num==32 /*Congo, The Democratic Republic of the*/
replace eu=0 if cntbrthc_num==33 /*Central African Republic*/
replace eu=0 if cntbrthc_num==34 /*Congo*/
replace eu=1 if cntbrthc_num==35 /*Switzerland*/
replace eu=0 if cntbrthc_num==36 /*Cote Ivoire*/
replace eu=0 if cntbrthc_num==37 /*Chile*/
replace eu=0 if cntbrthc_num==38 /*Cameroon*/
replace eu=0 if cntbrthc_num==39 /*China*/
replace eu=0 if cntbrthc_num==40 /*Colombia*/
replace eu=0 if cntbrthc_num==41 /*Costa Rica*/
replace eu=0 if cntbrthc_num==42 /*Cuba*/
replace eu=0 if cntbrthc_num==43 /*Cape Verde*/
replace eu=0 if cntbrthc_num==44 /*Curacao*/
replace eu=1 if cntbrthc_num==45 /*Czech republic*/
replace eu=0 if cntbrthc_num==45 & year<2004 /*Czech republic*/
replace eu=1 if cntbrthc_num==46 /*Germany*/
replace eu=0 if cntbrthc_num==47 /*Djibouti*/
replace eu=1 if cntbrthc_num==48 /*Denmark*/
replace eu=0 if cntbrthc_num==48 & year<1973/*Denmark*/
replace eu=0 if cntbrthc_num==49 /*Dominica*/
replace eu=0 if cntbrthc_num==50 /*Dominican Republic*/
replace eu=0 if cntbrthc_num==51 /*Algeria*/
replace eu=0 if cntbrthc_num==52 /*Ecuador*/
replace eu=1 if cntbrthc_num==53 /*Estonia*/
replace eu=0 if cntbrthc_num==53 & year<2004 /*Estonia*/
replace eu=0 if cntbrthc_num==54 /*Egypt*/
replace eu=0 if cntbrthc_num==55 /*Eritrea*/
replace eu=1 if cntbrthc_num==56 /*Spain*/
replace eu=0 if cntbrthc_num==56 & year<1986 /*Spain*/
replace eu=0 if cntbrthc_num==57 /*Ethiopia*/
replace eu=1 if cntbrthc_num==58 /*Finland*/
replace eu=0 if cntbrthc_num==58 & year<1995 /*Finland*/
replace eu=0 if cntbrthc_num==59 /*Faroe Islands*/
replace eu=1 if cntbrthc_num==60 /*France*/
replace eu=0 if cntbrthc_num==61 /*Gabon*/
replace eu=1 if cntbrthc_num==62 /*Great Britain*/
replace eu=0 if cntbrthc_num==62 & year<1973 /*Great Britain*/
replace eu=0 if cntbrthc_num==63 /*Grenada*/
replace eu=0 if cntbrthc_num==64 /*Georgia*/
replace eu=0 if cntbrthc_num==65 /*French Guinea*/
replace eu=0 if cntbrthc_num==66 /*Ghana*/
replace eu=0 if cntbrthc_num==67 /*Greenland*/
replace eu=0 if cntbrthc_num==68 /*Gambia*/
replace eu=0 if cntbrthc_num==69 /*Guinea*/
replace eu=0 if cntbrthc_num==70 /*Guadalupe*/
replace eu=0 if cntbrthc_num==71 /*Equatorial Guinea*/
replace eu=1 if cntbrthc_num==72 /*Greece*/
replace eu=0 if cntbrthc_num==72 & year<1981 /*Greece*/
replace eu=0 if cntbrthc_num==73 /*Guatemala*/
replace eu=0 if cntbrthc_num==74 /*Guinea Bissau*/
replace eu=0 if cntbrthc_num==75 /*Guyana*/
replace eu=0 if cntbrthc_num==76 /*Hong Kong*/
replace eu=0 if cntbrthc_num==77 /*Honduras*/
replace eu=1 if cntbrthc_num==78 /*Croatia*/
replace eu=0 if cntbrthc_num==78 & year<2004 /*Croatia*/
replace eu=1 if cntbrthc_num==79 /*Hungary*/
replace eu=0 if cntbrthc_num==79 & year<2004 /*Hungary*/
replace eu=0 if cntbrthc_num==80 /*Indonesia*/
replace eu=1 if cntbrthc_num==81 /*Ireland*/
replace eu=0 if cntbrthc_num==81 & year<1973 /*Ireland*/
replace eu=0 if cntbrthc_num==82 /*Israel*/
replace eu=0 if cntbrthc_num==83 /*India*/
replace eu=0 if cntbrthc_num==84 /*Iraq*/
replace eu=0 if cntbrthc_num==85 /*Iran*/
replace eu=0 if cntbrthc_num==86 /*Iceland*/
replace eu=1 if cntbrthc_num==87 /*Italy*/
replace eu=1 if cntbrthc_num==88 /*Jersey (UK dependency)*/
replace eu=0 if cntbrthc_num==88 & year<1973 /*Jersey (UK dependency)*/
replace eu=0 if cntbrthc_num==89 /*Jamaica*/
replace eu=0 if cntbrthc_num==90 /*Jordan*/
replace eu=0 if cntbrthc_num==91 /*Japan*/
replace eu=0 if cntbrthc_num==92 /*Kenya*/
replace eu=0 if cntbrthc_num==93 /*Kyrgyzstan*/
replace eu=0 if cntbrthc_num==94 /*Cambodia*/
replace eu=0 if cntbrthc_num==95 /*Comoros*/
replace eu=0 if cntbrthc_num==96 /*Korea, Republic of*/
replace eu=0 if cntbrthc_num==97 /*Kuwait*/
replace eu=0 if cntbrthc_num==98 /*Kazakhstan*/
replace eu=0 if cntbrthc_num==99 /*Lao People's Democratic Republic*/
replace eu=0 if cntbrthc_num==100 /*Lebanon*/
replace eu=0 if cntbrthc_num==101 /*Saint Lucia*/
replace eu=0 if cntbrthc_num==102 /*Sri Lanka*/
replace eu=0 if cntbrthc_num==103 /*Liberia*/
replace eu=0 if cntbrthc_num==104 /*Lesotho*/
replace eu=1 if cntbrthc_num==105 /*Lithuania*/
replace eu=0 if cntbrthc_num==105 & year<2004 /*Lithuania*/
replace eu=1 if cntbrthc_num==106 /*Luxembourg*/
replace eu=1 if cntbrthc_num==107 /*Latvia*/
replace eu=0 if cntbrthc_num==107 & year<2004 /*Latvia*/
replace eu=0 if cntbrthc_num==108 /*Lybia*/
replace eu=0 if cntbrthc_num==109 /*Morocco*/
replace eu=0 if cntbrthc_num==110 /*Moldova, Republic of*/
replace eu=0 if cntbrthc_num==111 /*Madagascar*/
replace eu=0 if cntbrthc_num==112 /*Macedonia, The Former Yugoslav Republic of*/
replace eu=0 if cntbrthc_num==113 /*Mali*/
replace eu=0 if cntbrthc_num==114 /*Mongolia*/
replace eu=0 if cntbrthc_num==115 /*Martinique*/
replace eu=0 if cntbrthc_num==116 /*Mauritania*/
replace eu=1 if cntbrthc_num==117 /*Malta*/
replace eu=0 if cntbrthc_num==117 & year<2004 /*Malta*/
replace eu=0 if cntbrthc_num==118 /*Mauritius*/
replace eu=0 if cntbrthc_num==119 /*Malawi*/
replace eu=0 if cntbrthc_num==120 /*Mexico*/
replace eu=0 if cntbrthc_num==121 /*Malaysia*/
replace eu=0 if cntbrthc_num==122 /*Mozambique*/
replace eu=0 if cntbrthc_num==123 /*Niger*/
replace eu=0 if cntbrthc_num==124 /*Nigeria*/
replace eu=0 if cntbrthc_num==125 /*Nicaragua*/
replace eu=1 if cntbrthc_num==126 /*Netherlands*/
replace eu=1 if cntbrthc_num==127 /*Norway*/
replace eu=0 if cntbrthc_num==128 /*Nepal*/
replace eu=0 if cntbrthc_num==129 /*New Zealand*/
replace eu=0 if cntbrthc_num==130 /*Panama*/
replace eu=0 if cntbrthc_num==131 /*Peru*/
replace eu=0 if cntbrthc_num==132 /*French Polynesia*/
replace eu=0 if cntbrthc_num==133 /*Philipnnes*/
replace eu=0 if cntbrthc_num==134 /*Pakistan*/
replace eu=1 if cntbrthc_num==135 /*Poland*/
replace eu=0 if cntbrthc_num==135 & year<2004 /*Poland*/
replace eu=0 if cntbrthc_num==136 /*Puerto Rico*/
replace eu=0 if cntbrthc_num==137 /*Palestine*/
replace eu=1 if cntbrthc_num==138 /*Portugal*/
replace eu=0 if cntbrthc_num==138 & year<1986 /*Portugal*/
replace eu=0 if cntbrthc_num==139 /*Paraguay*/
replace eu=0 if cntbrthc_num==140 /*Reunion*/
replace eu=1 if cntbrthc_num==141 /*Romania*/
replace eu=0 if cntbrthc_num==141 & year<2004 /*Romania*/
replace eu=0 if cntbrthc_num==142 /*Serbia*/
replace eu=0 if cntbrthc_num==143 /*Russia*/
replace eu=0 if cntbrthc_num==144 /*Rwanda*/
replace eu=0 if cntbrthc_num==145 /*Saudi Arabia*/
replace eu=0 if cntbrthc_num==146 /*Seychelles*/
replace eu=0 if cntbrthc_num==147 /*Sudan*/
replace eu=1 if cntbrthc_num==148 /*Sweden*/
replace eu=0 if cntbrthc_num==148 & year<1995 /*Sweden*/
replace eu=0 if cntbrthc_num==149 /*Singapore*/
replace eu=1 if cntbrthc_num==150 /*Slovenia*/
replace eu=0 if cntbrthc_num==150 & year<2004 /*Slovenia*/
replace eu=1 if cntbrthc_num==151 /*Slovakia*/
replace eu=0 if cntbrthc_num==151 & year<2004 /*Slovakia*/
replace eu=0 if cntbrthc_num==152 /*Sierra Leone*/
replace eu=0 if cntbrthc_num==153 /*Senegal*/
replace eu=0 if cntbrthc_num==154 /*Somalia*/
replace eu=0 if cntbrthc_num==155 /*Suriname*/
replace eu=0 if cntbrthc_num==156 /*Sao Tome and Principe*/
replace eu=0 if cntbrthc_num==157 /*El Salvador*/
replace eu=0 if cntbrthc_num==158 /*Sint Maarten (Dutch Part)*/
replace eu=0 if cntbrthc_num==159 /*Syrian*/
replace eu=0 if cntbrthc_num==160 /*Chad*/
replace eu=0 if cntbrthc_num==161 /*Togo*/
replace eu=0 if cntbrthc_num==162 /*Thailand*/
replace eu=0 if cntbrthc_num==163 /*Tajikistan*/
replace eu=0 if cntbrthc_num==164 /*Turkmenistan*/
replace eu=0 if cntbrthc_num==165 /*Tunisia*/
replace eu=0 if cntbrthc_num==166 /*Turkey*/
replace eu=0 if cntbrthc_num==167 /*Trinidad and Tobago*/
replace eu=0 if cntbrthc_num==168 /*Taiwan*/
replace eu=0 if cntbrthc_num==169 /*Tanzania*/
replace eu=0 if cntbrthc_num==170 /*Ukraine*/
replace eu=0 if cntbrthc_num==171 /*Uganda*/
replace eu=0 if cntbrthc_num==172 /*US*/
replace eu=0 if cntbrthc_num==173 /*Uruguay*/
replace eu=0 if cntbrthc_num==174 /*Uzbekistan*/
replace eu=0 if cntbrthc_num==175 /*Venezuela*/
replace eu=0 if cntbrthc_num==176 /*Vietnam*/
replace eu=0 if cntbrthc_num==177 /*Kosovo*/
replace eu=0 if cntbrthc_num==178 /*Mayotte*/
replace eu=0 if cntbrthc_num==179 /*South Africa*/
replace eu=0 if cntbrthc_num==180 /*Zambia*/
replace eu=0 if cntbrthc_num==181 /*Zimbabwe*/


*Country of origin variables

gen codecow2=.
replace codecow2=315 if cntbrthb_num==2 /*Czechoslovakia*/
replace codecow2=345 if cntbrthb_num==3 /*Yugoslavia*/
replace codecow2=345 if cntbrthb_num==4 /*Serbia and Montenegro*/
replace codecow2=700 if cntbrthb_num==8 /*Afghanistan*/
replace codecow2=339 if cntbrthb_num==9 /*Albania*/
replace codecow2=371 if cntbrthb_num==10 /*Armenia*/
replace codecow2=540 if cntbrthb_num==12 /*Angola*/
replace codecow2=160 if cntbrthb_num==13 /*Argentina*/
replace codecow2=305 if cntbrthb_num==14 /*Austria*/
replace codecow2=900 if cntbrthb_num==15 /*Australia*/
replace codecow2=373 if cntbrthb_num==18 /*Azerbaijan*/
replace codecow2=346 if cntbrthb_num==19 /*Bosnia and Herzegovina*/
replace codecow2=771 if cntbrthb_num==20 /*Bangladesh*/
replace codecow2=211 if cntbrthb_num==21 /*Belgium*/
replace codecow2=439 if cntbrthb_num==22 /*Burkina Faso*/
replace codecow2=355 if cntbrthb_num==23 /*Bulgaria*/
replace codecow2=516 if cntbrthb_num==24 /*Burundi*/
replace codecow2=434 if cntbrthb_num==25 /*Benin*/
replace codecow2=835 if cntbrthb_num==26 /*Brunei Darussalam*/
replace codecow2=145 if cntbrthb_num==27 /*Bolivia*/
replace codecow2=140 if cntbrthb_num==28 /*Brazil*/
replace codecow2=571 if cntbrthb_num==29 /*Botswana*/
replace codecow2=370 if cntbrthb_num==30 /*Belarus*/
replace codecow2=20 if cntbrthb_num==31 /*Canada*/
replace codecow2=490 if cntbrthb_num==32 /*Democratic Republic Congo*/
replace codecow2=482 if cntbrthb_num==33 /*Central African Republic*/
replace codecow2=484 if cntbrthb_num==34 /*Congo*/
replace codecow2=225 if cntbrthb_num==35 /*Switzerland: not codecow2 but we consider so due to similar arrangements*/
replace codecow2=437 if cntbrthb_num==36 /*Cote Ivoire*/
replace codecow2=155 if cntbrthb_num==37 /*Chile*/
replace codecow2=471 if cntbrthb_num==38 /*Cameroon*/
replace codecow2=710 if cntbrthb_num==39 /*China*/
replace codecow2=100 if cntbrthb_num==40 /*Colombia*/
replace codecow2=40 if cntbrthb_num==41 /*Cuba*/
replace codecow2=402 if cntbrthb_num==42 /*Cape Verde*/
replace codecow2=352 if cntbrthb_num==43 /*Cyprus*/
replace codecow2=316 if cntbrthb_num==44 /*Czech Republic*/
replace codecow2=255 if cntbrthb_num==45 /*Germany*/
replace codecow2=390 if cntbrthb_num==46 /*Denmark*/
replace codecow2=42 if cntbrthb_num==47 /*Dominican Republic*/
replace codecow2=615 if cntbrthb_num==48 /*Algeria*/
replace codecow2=130 if cntbrthb_num==49 /*Ecuador*/
replace codecow2=366 if cntbrthb_num==50 /*Estonia*/
replace codecow2=651 if cntbrthb_num==51 /*Egypt*/
replace codecow2=531 if cntbrthb_num==52 /*Eritrea*/
replace codecow2=230 if cntbrthb_num==53 /*Spain*/
replace codecow2=530 if cntbrthb_num==54 /*Ethiopia*/
replace codecow2=375 if cntbrthb_num==55 /*Finland*/
replace codecow2=220 if cntbrthb_num==57 /*France*/
replace codecow2=481 if cntbrthb_num==58 /*Gabon*/
replace codecow2=200 if cntbrthb_num==59 /*Great Britain*/
replace codecow2=372 if cntbrthb_num==60 /*Georgia*/
replace codecow2=452 if cntbrthb_num==61 /*Ghana*/
replace codecow2=420 if cntbrthb_num==63 /*Gambia*/
replace codecow2=438 if cntbrthb_num==64 /*Guinea*/
replace codecow2=350 if cntbrthb_num==66 /*Greece*/
replace codecow2=90 if cntbrthb_num==67 /*Guatemala*/
replace codecow2=404 if cntbrthb_num==68 /*Guinea-Bissau*/
replace codecow2=110 if cntbrthb_num==69 /*Guyana*/
replace codecow2=997 if cntbrthb_num==70 /*Hong Kong*/
replace codecow2=91 if cntbrthb_num==71 /*Honduras*/
replace codecow2=344 if cntbrthb_num==72 /*Croatia*/
replace codecow2=310 if cntbrthb_num==73 /*Hungary*/
replace codecow2=850 if cntbrthb_num==74 /*Indonesia*/
replace codecow2=205 if cntbrthb_num==75 /*Ireland*/
replace codecow2=666 if cntbrthb_num==76 /*Israel*/
replace codecow2=750 if cntbrthb_num==77 /*India*/
replace codecow2=645 if cntbrthb_num==78 /*Iraq*/
replace codecow2=630 if cntbrthb_num==79 /*Iran*/
replace codecow2=395 if cntbrthb_num==80 /*Iceland*/
replace codecow2=325 if cntbrthb_num==81 /*Italy*/
replace codecow2=51 if cntbrthb_num==83 /*Jamaica*/
replace codecow2=663 if cntbrthb_num==84 /*Jordan*/
replace codecow2=740 if cntbrthb_num==85 /*Japan*/
replace codecow2=501 if cntbrthb_num==86 /*Kenya*/
replace codecow2=703 if cntbrthb_num==87 /*Kyrgyzstan*/
replace codecow2=811 if cntbrthb_num==88 /*Cambodia*/
replace codecow2=581 if cntbrthb_num==89 /*Comoros*/
replace codecow2=731 if cntbrthb_num==90 /*Korea, Democratic People's Republic of*/
replace codecow2=732 if cntbrthb_num==91 /*Korea, Republic of*/
replace codecow2=690 if cntbrthb_num==92 /*Kuwait*/
replace codecow2=705 if cntbrthb_num==93 /*Kazakhstan*/
replace codecow2=812 if cntbrthb_num==94 /*Lao People's Democratic Republic*/
replace codecow2=660 if cntbrthb_num==95 /*Lebanon*/
replace codecow2=56 if cntbrthb_num==96 /*Saint Lucia*/
replace codecow2=223 if cntbrthb_num==97 /*Liechtenstein*/
replace codecow2=780 if cntbrthb_num==98 /*Sri Lanka*/
replace codecow2=450 if cntbrthb_num==99 /*Liberia*/
replace codecow2=368 if cntbrthb_num==100 /*Lithuania*/
replace codecow2=212 if cntbrthb_num==101 /*Luxembourg*/
replace codecow2=367 if cntbrthb_num==102 /*Latvia*/
replace codecow2=620 if cntbrthb_num==103 /*Libyan Arab Jamahiriya*/
replace codecow2=600 if cntbrthb_num==104 /*Morocco*/
replace codecow2=359 if cntbrthb_num==105 /*Moldova*/
replace codecow2=341 if cntbrthb_num==106 /*Montenegro*/
replace codecow2=580 if cntbrthb_num==107 /*Madagascar*/
replace codecow2=343 if cntbrthb_num==108 /*Macedonia*/
replace codecow2=432 if cntbrthb_num==109 /*Mali*/
replace codecow2=710 if cntbrthb_num==110 /*Macao*/
replace codecow2=338 if cntbrthb_num==113 /*Malta*/
replace codecow2=590 if cntbrthb_num==114 /*Mauritius*/
replace codecow2=70 if cntbrthb_num==115 /*Mexico*/
replace codecow2=820 if cntbrthb_num==116 /*Malaysia*/
replace codecow2=541 if cntbrthb_num==117 /*Mozambique*/
replace codecow2=565 if cntbrthb_num==118 /*Namibia*/
replace codecow2=475 if cntbrthb_num==119 /*Nigeria*/
replace codecow2=93 if cntbrthb_num==120 /*Nicaragua*/
replace codecow2=210 if cntbrthb_num==121 /*Netherlands*/
replace codecow2=385 if cntbrthb_num==122 /*Norway*/
replace codecow2=790 if cntbrthb_num==123 /*Nepal*/
replace codecow2=920 if cntbrthb_num==124 /*New Zealand*/
replace codecow2=95 if cntbrthb_num==125 /*Panama*/
replace codecow2=135 if cntbrthb_num==126 /*Peru*/
replace codecow2=840 if cntbrthb_num==127 /*Philipines*/
replace codecow2=770 if cntbrthb_num==128 /*Pakistan*/
replace codecow2=290 if cntbrthb_num==129 /*Poland*/
replace codecow2=666 if cntbrthb_num==130 /*Palestina (Israel)*/
replace codecow2=235 if cntbrthb_num==131 /*Portugal*/
replace codecow2=150 if cntbrthb_num==132 /*Paraguay*/
replace codecow2=360 if cntbrthb_num==134 /*Romania*/
replace codecow2=345 if cntbrthb_num==135 /*Serbia*/
replace codecow2=365 if cntbrthb_num==136 /*Russia*/
replace codecow2=517 if cntbrthb_num==137 /*Rwanda*/
replace codecow2=670 if cntbrthb_num==138 /*Saudi Arabia*/
replace codecow2=625 if cntbrthb_num==139 /*Sudan*/
replace codecow2=380 if cntbrthb_num==140 /*Sweden*/
replace codecow2=830 if cntbrthb_num==141 /*Singapore*/
replace codecow2=349 if cntbrthb_num==142 /*Slovenia*/
replace codecow2=317 if cntbrthb_num==143 /*Slovakia*/
replace codecow2=451 if cntbrthb_num==144 /*Sierra Leone*/
replace codecow2=433 if cntbrthb_num==145 /*Senegal*/
replace codecow2=520 if cntbrthb_num==146 /*Somalia*/
replace codecow2=115 if cntbrthb_num==147 /*Suriname*/
replace codecow2=403 if cntbrthb_num==148 /*Sao Tome and Principe*/
replace codecow2=92 if cntbrthb_num==149 /*El Salvador*/
replace codecow2=652 if cntbrthb_num==150 /*Syria*/
replace codecow2=461 if cntbrthb_num==151 /*Togo*/
replace codecow2=800 if cntbrthb_num==152 /*Thailand*/
replace codecow2=702 if cntbrthb_num==153 /*Tajikistan*/
replace codecow2=616 if cntbrthb_num==154 /*Tunisia*/
replace codecow2=640 if cntbrthb_num==155 /*Turkey*/
replace codecow2=52 if cntbrthb_num==156 /*Trinidad and Tobago*/
replace codecow2=713 if cntbrthb_num==157 /*Taiwan*/
replace codecow2=510 if cntbrthb_num==158 /*Tanzania*/
replace codecow2=369 if cntbrthb_num==159 /*Ukraine*/
replace codecow2=500 if cntbrthb_num==160 /*Uganda*/
replace codecow2=2 if cntbrthb_num==161 /*United States*/
replace codecow2=165 if cntbrthb_num==162 /*Uruguay*/
replace codecow2=704 if cntbrthb_num==163 /*Uzbekistan*/
replace codecow2=101 if cntbrthb_num==164 /*Venezuela*/
replace codecow2=816 if cntbrthb_num==165 /*Vietnam*/
replace codecow2=679 if cntbrthb_num==166 /*Yemen*/
replace codecow2=560 if cntbrthb_num==167 /*South Africa*/
replace codecow2=551 if cntbrthb_num==168 /*Zambia*/
replace codecow2=552 if cntbrthb_num==169 /*Zimbabwe*/


replace codecow2=265 if cntbrthc_num==1 /*DDR*/
replace codecow2=315 if cntbrthc_num==3 /*Czechoslovakia*/
replace codecow2=345 if cntbrthc_num==4 /*Yugoslavia*/
replace codecow2=345 if cntbrthc_num==5 /*Serbia and Montenegro*/
replace codecow2=696 if cntbrthc_num==9 /*United Arab Emirates*/
replace codecow2=700 if cntbrthc_num==10 /*Afghanistan*/
replace codecow2=339 if cntbrthc_num==11 /*Albania*/
replace codecow2=371 if cntbrthc_num==12 /*Armenia*/
replace codecow2=540 if cntbrthc_num==13 /*Angola*/
replace codecow2=160 if cntbrthc_num==14 /*Argentina*/
replace codecow2=305 if cntbrthc_num==15 /*Austria*/
replace codecow2=900 if cntbrthc_num==16 /*Australia*/
replace codecow2=373 if cntbrthc_num==18 /*Azerbajan*/
replace codecow2=346 if cntbrthc_num==19 /*Bosnia and Herzegovina*/
replace codecow2=771 if cntbrthc_num==20 /*Bangladesh*/
replace codecow2=211 if cntbrthc_num==21 /*Belgium*/
replace codecow2=439 if cntbrthc_num==22 /*Burkina Faso*/
replace codecow2=355 if cntbrthc_num==23 /*Bulgaria*/
replace codecow2=516 if cntbrthc_num==24 /*Burundi*/
replace codecow2=434 if cntbrthc_num==25 /*Benin*/
replace codecow2=835 if cntbrthc_num==26 /*Brunei Darussalam*/
replace codecow2=145 if cntbrthc_num==27 /*Bolivia*/
replace codecow2=140 if cntbrthc_num==29 /*Brazil*/
replace codecow2=370 if cntbrthc_num==30 /*Belarus*/
replace codecow2=20 if cntbrthc_num==31 /*Canada*/
replace codecow2=490 if cntbrthc_num==32 /*Congo, The Democratic Republic of the*/
replace codecow2=482 if cntbrthc_num==33 /*Central African Republic*/
replace codecow2=484 if cntbrthc_num==34 /*Congo*/
replace codecow2=225 if cntbrthc_num==35 /*Switzerland*/
replace codecow2=437 if cntbrthc_num==36 /*Cote Ivoire*/
replace codecow2=155 if cntbrthc_num==37 /*Chile*/
replace codecow2=471 if cntbrthc_num==38 /*Cameroon*/
replace codecow2=710 if cntbrthc_num==39 /*China*/
replace codecow2=100 if cntbrthc_num==40 /*Colombia*/
replace codecow2=94 if cntbrthc_num==41 /*Costa Rica*/
replace codecow2=40 if cntbrthc_num==42 /*Cuba*/
replace codecow2=402 if cntbrthc_num==43 /*Cape Verde*/
replace codecow2=316 if cntbrthc_num==45 /*Czech republic*/
replace codecow2=255 if cntbrthc_num==46 /*Germany*/
replace codecow2=522 if cntbrthc_num==47 /*Djibouti*/
replace codecow2=390 if cntbrthc_num==48 /*Denmark*/
replace codecow2=54 if cntbrthc_num==49 /*Dominica*/
replace codecow2=42 if cntbrthc_num==50 /*Dominican Republic*/
replace codecow2=615 if cntbrthc_num==51 /*Algeria*/
replace codecow2=130 if cntbrthc_num==52 /*Ecuador*/
replace codecow2=366 if cntbrthc_num==53 /*Estonia*/
replace codecow2=651 if cntbrthc_num==54 /*Egypt*/
replace codecow2=531 if cntbrthc_num==55 /*Eritrea*/
replace codecow2=230 if cntbrthc_num==56 /*Spain*/
replace codecow2=530 if cntbrthc_num==57 /*Ethiopia*/
replace codecow2=375 if cntbrthc_num==58 /*Finland*/
replace codecow2=220 if cntbrthc_num==60 /*France*/
replace codecow2=481 if cntbrthc_num==61 /*Gabon*/
replace codecow2=200 if cntbrthc_num==62 /*Great Britain*/
replace codecow2=55 if cntbrthc_num==63 /*Grenada*/
replace codecow2=372 if cntbrthc_num==64 /*Georgia*/
replace codecow2=438 if cntbrthc_num==65 /*French Guinea*/
replace codecow2=452 if cntbrthc_num==66 /*Ghana*/
replace codecow2=420 if cntbrthc_num==68 /*Gambia*/
replace codecow2=438 if cntbrthc_num==69 /*Guinea*/
replace codecow2=411 if cntbrthc_num==71 /*Equatorial Guinea*/
replace codecow2=350 if cntbrthc_num==72 /*Greece*/
replace codecow2=90 if cntbrthc_num==73 /*Guatemala*/
replace codecow2=404 if cntbrthc_num==74 /*Guinea Bissau*/
replace codecow2=110 if cntbrthc_num==75 /*Guyana*/
replace codecow2=997 if cntbrthc_num==76 /*Hong Kong*/
replace codecow2=91 if cntbrthc_num==77 /*Honduras*/
replace codecow2=344 if cntbrthc_num==78 /*Croatia*/
replace codecow2=310 if cntbrthc_num==79 /*Hungary*/
replace codecow2=850 if cntbrthc_num==80 /*Indonesia*/
replace codecow2=205 if cntbrthc_num==81 /*Ireland*/
replace codecow2=666 if cntbrthc_num==82 /*Israel*/
replace codecow2=750 if cntbrthc_num==83 /*India*/
replace codecow2=645 if cntbrthc_num==84 /*Iraq*/
replace codecow2=630 if cntbrthc_num==85 /*Iran*/
replace codecow2=395 if cntbrthc_num==86 /*Iceland*/
replace codecow2=325 if cntbrthc_num==87 /*Italy*/
replace codecow2=51 if cntbrthc_num==89 /*Jamaica*/
replace codecow2=663 if cntbrthc_num==90 /*Jordan*/
replace codecow2=740 if cntbrthc_num==91 /*Japan*/
replace codecow2=501 if cntbrthc_num==92 /*Kenya*/
replace codecow2=703 if cntbrthc_num==93 /*Kyrgyzstan*/
replace codecow2=811 if cntbrthc_num==94 /*Cambodia*/
replace codecow2=581 if cntbrthc_num==95 /*Comoros*/
replace codecow2=732 if cntbrthc_num==96 /*Korea, Republic of*/
replace codecow2=690 if cntbrthc_num==97 /*Kuwait*/
replace codecow2=705 if cntbrthc_num==98 /*Kazakhstan*/
replace codecow2=812 if cntbrthc_num==99 /*Lao People's Democratic Republic*/
replace codecow2=660 if cntbrthc_num==100 /*Lebanon*/
replace codecow2=56 if cntbrthc_num==101 /*Saint Lucia*/
replace codecow2=780 if cntbrthc_num==102 /*Sri Lanka*/
replace codecow2=450 if cntbrthc_num==103 /*Liberia*/
replace codecow2=570 if cntbrthc_num==104 /*Lesotho*/
replace codecow2=368 if cntbrthc_num==105 /*Lithuania*/
replace codecow2=212 if cntbrthc_num==106 /*Luxembourg*/
replace codecow2=367 if cntbrthc_num==107 /*Latvia*/
replace codecow2=620 if cntbrthc_num==108 /*Lybia*/
replace codecow2=600 if cntbrthc_num==109 /*Morocco*/
replace codecow2=359 if cntbrthc_num==110 /*Moldova, Republic of*/
replace codecow2=580 if cntbrthc_num==111 /*Madagascar*/
replace codecow2=343 if cntbrthc_num==112 /*Macedonia, The Former Yugoslav Republic of*/
replace codecow2=432 if cntbrthc_num==113 /*Mali*/
replace codecow2=712 if cntbrthc_num==114 /*Mongolia*/
replace codecow2=435 if cntbrthc_num==116 /*Mauritania*/
replace codecow2=338 if cntbrthc_num==117 /*Malta*/
replace codecow2=590 if cntbrthc_num==118 /*Mauritius*/
replace codecow2=553 if cntbrthc_num==119 /*Malawi*/
replace codecow2=70 if cntbrthc_num==120 /*Mexico*/
replace codecow2=820 if cntbrthc_num==121 /*Malaysia*/
replace codecow2=541 if cntbrthc_num==122 /*Mozambique*/
replace codecow2=436 if cntbrthc_num==123 /*Niger*/
replace codecow2=475 if cntbrthc_num==124 /*Nigeria*/
replace codecow2=93 if cntbrthc_num==125 /*Nicaragua*/
replace codecow2=210 if cntbrthc_num==126 /*Netherlands*/
replace codecow2=385 if cntbrthc_num==127 /*Norway*/
replace codecow2=790 if cntbrthc_num==128 /*Nepal*/
replace codecow2=920 if cntbrthc_num==129 /*New Zealand*/
replace codecow2=95 if cntbrthc_num==130 /*Panama*/
replace codecow2=135 if cntbrthc_num==131 /*Peru*/
replace codecow2=840 if cntbrthc_num==133 /*Philipnnes*/
replace codecow2=770 if cntbrthc_num==134 /*Pakistan*/
replace codecow2=290 if cntbrthc_num==135 /*Poland*/
replace codecow2=666 if cntbrthc_num==137 /*Palestine (israel)*/
replace codecow2=235 if cntbrthc_num==138 /*Portugal*/
replace codecow2=150 if cntbrthc_num==139 /*Paraguay*/
replace codecow2=360 if cntbrthc_num==141 /*Romania*/
replace codecow2=345 if cntbrthc_num==142 /*Serbia*/
replace codecow2=365 if cntbrthc_num==143 /*Russia*/
replace codecow2=517 if cntbrthc_num==144 /*Rwanda*/
replace codecow2=670 if cntbrthc_num==145 /*Saudi Arabia*/
replace codecow2=591 if cntbrthc_num==146 /*Seychelles*/
replace codecow2=625 if cntbrthc_num==147 /*Sudan*/
replace codecow2=380 if cntbrthc_num==148 /*Sweden*/
replace codecow2=830 if cntbrthc_num==149 /*Singapore*/
replace codecow2=349 if cntbrthc_num==150 /*Slovenia*/
replace codecow2=317 if cntbrthc_num==151 /*Slovakia*/
replace codecow2=451 if cntbrthc_num==152 /*Sierra Leone*/
replace codecow2=433 if cntbrthc_num==153 /*Senegal*/
replace codecow2=520 if cntbrthc_num==154 /*Somalia*/
replace codecow2=115 if cntbrthc_num==155 /*Suriname*/
replace codecow2=403 if cntbrthc_num==156 /*Sao Tome and Principe*/
replace codecow2=92 if cntbrthc_num==157 /*El Salvador*/
replace codecow2=652 if cntbrthc_num==159 /*Syrian*/
replace codecow2=483 if cntbrthc_num==160 /*Chad*/
replace codecow2=461 if cntbrthc_num==161 /*Togo*/
replace codecow2=800 if cntbrthc_num==162 /*Thailand*/
replace codecow2=702 if cntbrthc_num==163 /*Tajikistan*/
replace codecow2=701 if cntbrthc_num==164 /*Turkmenistan*/
replace codecow2=616 if cntbrthc_num==165 /*Tunisia*/
replace codecow2=640 if cntbrthc_num==166 /*Turkey*/
replace codecow2=52 if cntbrthc_num==167 /*Trinidad and Tobago*/
replace codecow2=713 if cntbrthc_num==168 /*Taiwan*/
replace codecow2=510 if cntbrthc_num==169 /*Tanzania*/
replace codecow2=369 if cntbrthc_num==170 /*Ukraine*/
replace codecow2=500 if cntbrthc_num==171 /*Uganda*/
replace codecow2=2 if cntbrthc_num==172 /*US*/
replace codecow2=165 if cntbrthc_num==173 /*Uruguay*/
replace codecow2=704 if cntbrthc_num==174 /*Uzbekistan*/
replace codecow2=101 if cntbrthc_num==175 /*Venezuela*/
replace codecow2=816 if cntbrthc_num==176 /*Vietnam*/
replace codecow2=347 if cntbrthc_num==177 /*Kosovo*/
replace codecow2=560 if cntbrthc_num==179 /*South Africa*/
replace codecow2=551 if cntbrthc_num==180 /*Zambia*/
replace codecow2=552 if cntbrthc_num==181 /*Zimbabwe*/


capture drop _merge
merge m:m codecow2 year using qog_std_ts_jan20.dta,  keepusing(wdi_gdppppcon2011 wdi_unempilo wdi_chexppgdp wdi_expedu vdem_libdem )

gen gdp_ratio=gdp/wdi_gdppppcon2011
gen unemploy_ratio=unemp_rate/wdi_unempilo
gen healthexpend_ratio=health_expend/wdi_chexppgdp
gen educexpend_ratio=education_expend/wdi_expedu


gen female=gndr==2
gen age2=agea^2





gen length_stay=inwyys-year /*length of stay*/
replace length_stay=inwyr-year if essround==1
replace length_stay=inwyr-year if essround==2

drop if length_stay<0

sum length_stay, d
gen length_stay_rec=(length_stay-r(min))/(r(max)-r(min))

gen age_migr=year-yrbrn /*age when migrating*/
*drop if age_migr <18



egen country_year=group(country essround), label


capture drop external_cat
xtile external_cat=AvgS_ExtReg_B, nq(10)
xtile internal_cat=AvgS_IntReg_B, nq(10)
gen interaction=external_cat*internal_cat

gen external = AvgS_ExtReg_A+AvgS_ExtReg_B+AvgS_ExtReg_C

gen internal = AvgS_IntReg_A+AvgS_IntReg_B+AvgS_IntReg_C

recode brncntr 2=1 1=0, gen(migrant)

label define migr 1"Migrant" 0"Native"
label val migrant migr

gen origin=.
replace origin=1 if migrant==0
replace origin=2 if eu==1
replace origin=3 if eu==0

label define origin 1"Native" 2"EU" 3"Non-EU"
label val origin origin

gen west=0
replace west=1 if country==1
replace west=1 if country==2
replace west=1 if country==4
replace west=1 if country==7
replace west=1 if country==8
replace west=1 if country==10
replace west=1 if country==11
replace west=1 if country==12
replace west=1 if country==13
replace west=1 if country==14
replace west=1 if country==17
replace west=1 if country==19
replace west=1 if country==20
replace west=1 if country==22
replace west=1 if country==23
replace west=1 if country==24
replace west=1 if country==26
replace west=1 if country==28



save ESS_IMPIC_migrants.dta, replace


set scheme gg_tableau

*ANALYSES: code to replicate Figures 4 and 5, and Appendix P

gen where0=-0.5
gen where=-2
gen where2=-3
gen where5=-5
gen pipe = "|"



mixed stfdem c.external##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 || country_year:, mle variance 
est store model1

mixed stfdem c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2   || country_year:, mle variance 
est store model2

mixed stfdem c.external##c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 || country_year:, mle variance 
est store model3

margins, dydx(c.external) at(internal=(0.5(0.5)3) migrant=(0 1))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

 graph export "$pathfig/ess_satdem_natives.pdf", as(pdf) replace


est restore model1
margins, at(external=(0.2(0.5)3) migrant=(1))
marginsplot, title("") ytitle("Predicted satisfaction democracy", size(6))  ///
xtitle("Migrant selectivity", size(6))  recastci(rspike) ylabel(0(1)10)

 graph export "$pathfig/ess_satdem_predicted_external_natives.pdf", as(pdf) replace


est restore model2
margins, at(internal=(0.5(0.5)3) migrant=(1))
marginsplot, title("") ytitle("Predicted satisfaction democracy", size(6))  ///
xtitle("Strictness internal regulations", size(6))  recastci(rspike) ylabel(0(1)10) 

 graph export "$pathfig/ess_satdem_predicted_internal_natives.pdf", as(pdf) replace


est restore model3
margins, at(internal=(0.5(0.5)3) external=(0.2 3)  migrant=(1))
marginsplot, title("") ytitle("Predicted satisfaction democracy", size(6))  ///
xtitle("Strictness internal regulations", size(6))   recastci(rspike) ylabel(0(1)10)  legend(order(1 "Weak selectivity" 2 "Strong selectivity"))

graph export "$pathfig/ess_satdem_predicted_coconut_natives.pdf", as(pdf) replace



mixed trust_politicians c.external##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 || country_year:, mle variance 
est store model4
mixed trust_politicians c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 || country_year:, mle variance 
est store model5
mixed trust_politicians c.external##c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2  || country_year:, mle variance 
est store model6

margins, dydx(c.external) at(internal=(0.5(0.5)3) migrant=(0 1))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_poltrust_natives.pdf", as(pdf) replace

est restore model4
margins, at(external=(0.2(0.5)3) migrant=(1))
marginsplot, title("") ytitle("Predicted political trust", size(6))  ///
xtitle("Migrant selectivity", size(6))  recastci(rspike) ylabel(0(10)30)

 graph export "$pathfig/ess_poltrust_predicted_external_natives.pdf", as(pdf) replace


est restore model5
margins, at(internal=(0.5(0.5)3) migrant=(1))
marginsplot, title("") ytitle("Predicted political trust", size(6))  ///
xtitle("Strictness internal regulations", size(6))  recastci(rspike) ylabel(0(10)30) 

 graph export "$pathfig/ess_poltrust_predicted_internal_natives.pdf", as(pdf) replace


est restore model6
margins, at(internal=(0.5(0.5)3) external=(0.2 3)  migrant=(1))
marginsplot, title("") ytitle("Predicted political trust", size(6))  ///
xtitle("Strictness internal regulations", size(6))   recastci(rspike) ylabel(0(10)30) legend(order(1 "Weak selectivity" 2 "Strong selectivity"))

 graph export "$pathfig/ess_poltrust_predicted_coconut_natives.pdf", as(pdf) replace



mixed trust_legal c.external##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 || country_year:, mle variance 
est store model7
mixed trust_legal c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 || country_year:, mle variance 
est store model8
mixed trust_legal c.external##c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 || country_year:, mle variance 
est store model9

margins, dydx(c.external) at(internal=(0.5(0.5)3) migrant=(0 1))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_legaltrust_natives.pdf", as(pdf) replace


est restore model7
margins, at(external=(0.2(0.5)3) migrant=(1))
marginsplot, title("") ytitle("Predicted trust legal system", size(6))  ///
xtitle("Migrant selectivity", size(6))  recastci(rspike) ylabel(0(10)20)

 graph export "$pathfig/ess_legaltrust_predicted_external_natives.pdf", as(pdf) replace


est restore model8
margins, at(internal=(0.5(0.5)3) migrant=(1))
marginsplot, title("") ytitle("Predicted trust legal system", size(6))  ///
xtitle("Strictness internal regulations", size(6))  recastci(rspike) ylabel(0(10)20) 

 graph export "$pathfig/ess_legaltrust_predicted_internal_natives.pdf", as(pdf) replace


est restore model9
margins, at(internal=(0.5(0.5)3) external=(0.2 3)  migrant=(1))
marginsplot, title("") ytitle("Predicted trust legal system", size(6))  ///
xtitle("Strictness internal regulations", size(6))   recastci(rspike) ylabel(0(10)20) legend(order(1 "Weak selectivity" 2 "Strong selectivity"))

 graph export "$pathfig/ess_legaltrust_predicted_coconut_natives.pdf", as(pdf) replace


est restore model1
outreg2  using "${results}ess_satdem_natives_models.xls",  bdec(3) sdec(3) append
est restore model2
outreg2  using "${results}ess_satdem_natives_models.xls",  bdec(3) sdec(3) append
est restore model3
outreg2  using "${results}ess_satdem_natives_models.xls",  bdec(3) sdec(3) append 

est restore model4
outreg2  using "${results}ess_poltrust_natives_models.xls",  bdec(3) sdec(3) append
est restore model5
outreg2  using "${results}ess_poltrust_natives_models.xls",  bdec(3) sdec(3) append
est restore model6
outreg2  using "${results}ess_poltrust_natives_models.xls",  bdec(3) sdec(3) append 

est restore model7
outreg2  using "${results}ess_legal_natives_models.xls",  bdec(3) sdec(3) append
est restore model8
outreg2  using "${results}ess_legal_natives_models.xls",  bdec(3) sdec(3) append
est restore model9
outreg2  using "${results}ess_legal_natives_models.xls",  bdec(3) sdec(3) append 
 


*Code to replicate Figure 6, and Appendix Q

mixed stfdem external eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 
est store model10
mixed stfdem internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 
est store model11
mixed stfdem c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 
est store model12


margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(I, replace) recastci(rspike) yline(0, lc(black)) 


///
*addplot(scatter where internal, mcolor(none) mlabel(pipe) mlabc(black) ylabel(-2(1)2) xlabel(0.5(0.5)3) legend(off) below)

 graph export "$pathfig/ess_satdem_migrants.pdf", as(pdf) replace


mixed trust_politicians external eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 
est store model13
mixed trust_politicians internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 
est store model14
mixed trust_politicians c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 
est store model15

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(I, replace) recastci(rspike) yline(0, lc(black))

///
*addplot(scatter where internal, mcolor(none) mlabel(pipe) mlabc(black) ylabel(-2(1)6) xlabel(0.5(0.5)3) legend(off) below)

 graph export "$pathfig/ess_poltrust_migrants.pdf", as(pdf) replace



mixed trust_legal external eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 
est store model16
mixed trust_legal internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 
est store model17
mixed trust_legal c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 
est store model18

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(I, replace) recastci(rspike) yline(0, lc(black)) 

///
*addplot(scatter where internal, mcolor(none) mlabel(pipe) mlabc(black) ylabel(-2(1)4) xlabel(0.5(0.5)3) legend(off) below)

 graph export "$pathfig/ess_trustlegal_migrants.pdf", as(pdf) replace

est restore model10
outreg2  using "${results}ess_satdem_migrants_models.xls",  bdec(3) sdec(3) append
est restore model11
outreg2  using "${results}ess_satdem_migrants_models.xls",  bdec(3) sdec(3) append
est restore model12
outreg2  using "${results}ess_satdem_migrants_models.xls",  bdec(3) sdec(3) append 

est restore model13
outreg2  using "${results}ess_poltrust_migrants_models.xls",  bdec(3) sdec(3) append
est restore model14
outreg2  using "${results}ess_poltrust_migrants_models.xls",  bdec(3) sdec(3) append
est restore model15
outreg2  using "${results}ess_poltrust_migrants_models.xls",  bdec(3) sdec(3) append 

est restore model16
outreg2  using "${results}ess_legal_migrants_models.xls",  bdec(3) sdec(3) append
est restore model17
outreg2  using "${results}ess_legal_migrants_models.xls",  bdec(3) sdec(3) append
est restore model18
outreg2  using "${results}ess_legal_migrants_models.xls",  bdec(3) sdec(3) append 




*Appendix R - Figure A.3 (without fixed effects)

mixed stfdem c.external##c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl  if origin !=2 || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3) migrant=(0 1))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

 graph export "$pathfig/ess_satdem_natives_RE.pdf", as(pdf) replace


mixed trust_politicians c.external##c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl  if origin !=2  || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3) migrant=(0 1))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

 graph export "$pathfig/ess_poltrust_natives_RE.pdf", as(pdf) replace


mixed trust_legal c.external##c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl  if origin !=2 || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3) migrant=(0 1))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

 graph export "$pathfig/ess_legaltrust_natives_RE.pdf", as(pdf) replace
 
 
 *Appendix R - Figure A.4 (without fixed effects)

mixed stfdem c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl if migrant==1 & eu==0 || country_year:, mle variance 


margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(I, replace) recastci(rspike) yline(0, lc(black)) 


 graph export "$pathfig/ess_satdem_migrants_RE.pdf", as(pdf) replace



mixed trust_politicians c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl if migrant==1 & eu==0 || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(I, replace) recastci(rspike) yline(0, lc(black))


 graph export "$pathfig/ess_poltrust_migrants_RE.pdf", as(pdf) replace


mixed trust_legal c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl if migrant==1 & eu==0 || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(I, replace) recastci(rspike) yline(0, lc(black)) 

 graph export "$pathfig/ess_legaltrust_migrants_RE.pdf", as(pdf) replace

 

*Appendix S - selection mechanisms
*Non-economic mechanisms

*Individual stuff


capture drop _merge

merge m:m essround idno using "parents.dta",  keepusing(edulvlfa edulvlfb edulvlma edulvlmb)

numlabel edulvlfa, add
numlabel edulvlfb, add
numlabel edulvlma, add
numlabel edulvlmb, add

tab1 edulvlfa edulvlfb edulvlma edulvlmb

gen uni_father = .
replace uni_father = 1 if edulvlfa==5
replace uni_father = 0 if edulvlfa>=1 & edulvlfa<=4
replace uni_father = 1 if edulvlfb>=610 & edulvlfb<=800
replace uni_father = 0 if edulvlfb>=0 & edulvlfb<=520

gen uni_mother = .
replace uni_mother = 1 if edulvlma==5
replace uni_mother = 0 if edulvlma>=1 & edulvlfa<=4
replace uni_mother = 1 if edulvlmb>=610 & edulvlfb<=800
replace uni_mother = 0 if edulvlmb>=0 & edulvlfb<=520

gen uni_parents=.
replace uni_parents=1 if uni_father==1
replace uni_parents=1 if uni_mother==1
replace uni_parents=0 if uni_father==0
replace uni_parents=0 if uni_mother==0

mixed uni_parents c.external##c.internal  female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 


*mixed eduyrs c.external##c.internal  female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 


margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(F, replace) recastci(rspike) yline(0, lc(black))  

graph export "$pathfig/ess_mechanism_education.pdf", as(pdf) replace


mixed female c.external##c.internal eduyrs  agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 


margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(F, replace) recastci(rspike) yline(0, lc(black))  

///
*addplot(scatter where0 internal, mcolor(none) mlabel(pipe) mlabc(black) ylabel(-0.5 0 0.5) xlabel(0.5(0.5)3) legend(off) below)

graph export "$pathfig/ess_mechanism_gender.pdf", as(pdf) replace


mixed age_migr  c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance /*coconut slightly older migrants than tougher regimes*/

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(A, replace) recastci(rspike) yline(0, lc(black)) 

///
*addplot(scatter where internal, mcolor(none) mlabel(pipe) mlabc(black) ylabel(-2(5)30) xlabel(0.5(0.5)3) legend(off) below)

graph export "$pathfig/ess_mechanism_agemigr.pdf", as(pdf) replace



mixed length_stay  c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance /*tougher regimes people stay a bit longer than with coconut*/
gen where25=-25

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(L, replace) recastci(rspike) yline(0, lc(black)) 

///
*addplot(scatter where25 internal, mcolor(none) mlabel(pipe) mlabc(black) ylabel(-25(5)2) xlabel(0.5(0.5)3) legend(off) below)

graph export "$pathfig/ess_mechanism_length.pdf", as(pdf) replace


*Country of origin

mixed vdem_libdem  c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(D, replace) recastci(rspike) yline(0, lc(black))

graph export "$pathfig/ess_mechanism_democracy.pdf", as(pdf) replace


mixed wdi_gdppppcon2011  c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(G, replace) recastci(rspike) yline(0, lc(black))

graph export "$pathfig/ess_mechanism_gdp.pdf", as(pdf) replace


mixed wdi_unempilo  c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) ylabel (1(1)-5) yline(0) name(U, replace) recastci(rspike) yline(0, lc(black))

graph export "$pathfig/ess_mechanism_countryunemp.pdf", as(pdf) replace



capture drop _merge

gen COWcode = codecow2
merge m:m COWcode year using "V-Dem.dta",  keepusing(e_gdppc v2dlencmps v2x_libdem  )


mixed v2dlencmps  c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(U, replace) recastci(rspike) yline(0, lc(black))

graph export "$pathfig/ess_mechanism_publicgood.pdf", as(pdf) replace

 

*Appendix T - Economic mechanisms

*Figure A.7 - With fixed effects migrants vs migrants


mixed income c.AvgS_ExtReg_B##c.AvgS_IntReg_B eduyrs female agea age2 expenditure unemp_rate epl  i.country if migrant==1 & eu==0 & rtrd !=1  || country_year:, mle variance

margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1)) level(95)
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_income_migrants.pdf", as(pdf) replace


mixed permanent c.AvgS_ExtReg_B##c.AvgS_IntReg_B eduyrs female agea age2 expenditure unemp_rate epl  i.country if migrant==1 & eu==0 & rtrd !=1  || country_year:, mle variance 


margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1)) level(95)
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_permanent_migrants.pdf", as(pdf) replace



mixed unemp_3m c.AvgS_ExtReg_B##c.AvgS_IntReg_B eduyrs female agea age2 expenditure unemp_rate epl  i.country  if migrant==1 & eu==0 & rtrd !=1 || country_year:, mle variance 


margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1)) level(95)
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_unemp_migrants.pdf", as(pdf) replace


*Figure A.7 - With fixed effects migrants vs natives


mixed income c.AvgS_ExtReg_B##c.AvgS_IntReg_B##migrant eduyrs female agea age2 expenditure unemp_rate epl  i.country if origin !=2 & rtrd !=1  || country_year:, mle variance 


margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1) migrant=(0 1)) 
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_income_natives.pdf", as(pdf) replace



mixed permanent c.AvgS_ExtReg_B##c.AvgS_IntReg_B##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 & rtrd !=1  || country_year:, mle variance 


margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1) migrant=(0 1)) 
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_permanent_natives.pdf", as(pdf) replace



mixed unemp_3m c.AvgS_ExtReg_B##c.AvgS_IntReg_B##migrant eduyrs female agea age2 expenditure unemp_rate epl  i.country  if origin !=2 & rtrd !=1  || country_year:, mle variance 

margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1) migrant=(0 1)) 
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_unemp_natives.pdf", as(pdf) replace


*Economic mechanisms without country FE

*Figure A.6- economic mechanisms without country FE, Migrants vs migrants


mixed income c.AvgS_ExtReg_B##c.AvgS_IntReg_B eduyrs female agea age2 expenditure unemp_rate epl  if migrant==1 & eu==0 & rtrd !=1  || country_year:, mle variance 

margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1)) 
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_income_migrants_RE.pdf", as(pdf) replace


mixed permanent c.AvgS_ExtReg_B##c.AvgS_IntReg_B eduyrs female agea age2 expenditure unemp_rate epl  if migrant==1 & eu==0 & rtrd !=1  || country_year:, mle variance 


margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1)) 
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_permanent_migrants_RE.pdf", as(pdf) replace



mixed unemp_3m c.AvgS_ExtReg_B##c.AvgS_IntReg_B eduyrs female agea age2 expenditure unemp_rate epl   if migrant==1 & eu==0 & rtrd !=1 || country_year:, mle variance 


margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1)) 
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_unemp_migrants_RE.pdf", as(pdf) replace


*Figure A.6- economic mechanisms without country FE, Migrants vs natives

mixed income c.AvgS_ExtReg_B##c.AvgS_IntReg_B##migrant eduyrs female agea age2 expenditure unemp_rate epl  if origin !=2 & rtrd !=1  || country_year:, mle variance 


margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1) migrant=(0 1)) 
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_income_natives_RE.pdf", as(pdf) replace



mixed permanent c.AvgS_ExtReg_B##c.AvgS_IntReg_B##migrant eduyrs female agea age2 expenditure unemp_rate epl  if origin !=2 & rtrd !=1  || country_year:, mle variance /*not sign*/


margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1) migrant=(0 1)) 
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_permanent_natives_RE.pdf", as(pdf) replace



mixed unemp_3m c.AvgS_ExtReg_B##c.AvgS_IntReg_B##migrant eduyrs female agea age2 expenditure unemp_rate epl  i.country  if origin !=2 & rtrd !=1  || country_year:, mle variance /*yes but natives also affected by imm policy*/

margins, dydx(c.AvgS_ExtReg_B) at(AvgS_IntReg_B=(0(0.2)1) migrant=(0 1)) 
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_mechanism_unemp_natives_RE.pdf", as(pdf) replace




*Appendix A- Descriptive statistics


sum stfdem trust_politicians trust_legal income unemp_3m permanent migrant external internal eduyrs female agea age2 expenditure unemp_rate epl

*Appendix U - contemporaneous values of migration policy




cd "/Users/sergi/Library/CloudStorage/Dropbox/Work/Coconut paper/ESS data/"

global pathfig "/Users/sergi/Library/CloudStorage/Dropbox/Aplicaciones/Overleaf/Migrant political integration/"




*Add current year identifier in QoG data to match with ESS data later

use qog_std_ts_jan20.dta, clear

capture drop essround
gen essround=.
replace essround=1 if year==2002
replace essround=2 if year==2004
replace essround=3 if year==2006
replace essround=4 if year==2008
replace essround=5 if year==2010
replace essround=6 if year==2012
replace essround=7 if year==2014
replace essround=8 if year==2016

capture drop expenditure_current gdp_current unempl_current

gen expenditure_current=oecd_socexpnd_t1a
gen gdp_current=wdi_gdppppcon2011
gen unempl_current=wdi_unempilo

save qog_std_ts_jan20.dta, replace


*Reshape EPL data from OECD to merge with ESS later (strictness of employment protection - individual and collective dismissals (regular contracts)

import delimited using EPL.csv, clear

sort country time 
keep if series=="EPRC_V1"


capture drop essround
gen essround=.
replace essround=1 if time==2002
replace essround=2 if time==2004
replace essround=3 if time==2006
replace essround=4 if time==2008
replace essround=5 if time==2010
replace essround=6 if time==2012
replace essround=7 if time==2014
replace essround=8 if time==2016



rename country country_name

gen country=.
replace country=1 if v2=="Austria"
replace country=2 if v2=="Belgium"
replace country=4 if v2=="Switzerland"
replace country=6 if v2=="Czech Republic"
replace country=7 if v2=="Germany"
replace country=8 if v2=="Denmark"
replace country=9 if v2=="Estonia"
replace country=10 if v2=="Spain"
replace country=11 if v2=="Finland"
replace country=12 if v2=="France"
replace country=13 if v2=="United Kingdom"
replace country=14 if v2=="Greece"
replace country=16 if v2=="Hungary"
replace country=17 if v2=="Ireland"
replace country=19 if v2=="Iceland"
replace country=20 if v2=="Italy"
replace country=22 if v2=="Luxembourg"
replace country=23 if v2=="Netherlands"
replace country=24 if v2=="Norway"
replace country=25 if v2=="Poland"
replace country=26 if v2=="Portugal"
replace country=28 if v2=="Sweden"
replace country=30 if v2=="Slovak Republic"
replace country=31 if v2=="Turkey"

rename value epl

keep country essround epl


save EPL_OECD.dta, replace

*Use European Social Survey

use ESS1-8e01.dta, clear

encode cntry, gen(country)

numlabel country, add

tab country

* We remove Bulgaria (no data on IMPIC), Cyprus (no data on IMPIC), Croatia (no data on IMPIC), Israel (different immigration history), ///
/// Lithuania (no data on IMPIC), Russia (no data on IMPIC), Slovenia (no data on IMPIC), Ukraine (no data on IMPIC).

drop if country==3 | country==5 | country==15 | country==18 | country==21 | country==27 | country==29 | country==32

*We prepare the ESS data to match immigration policy upon year of arrival

recode country 1=1 2=3 4=7 6=9 7=10 8=11 9=13 10=15 11=16 12=17 13=18 14=19 16=20 17=21 19=23 ///
20=24 22=27 23=29 24=30 25=32 26=33 28=34 30=35 31=36, gen(cntryID)


gen ccodecow=.
replace ccodecow=305 if country==1
replace ccodecow=211 if country==2
replace ccodecow=225 if country==4
replace ccodecow=316 if country==6
replace ccodecow=255 if country==7
replace ccodecow=390 if country==8
replace ccodecow=366 if country==9
replace ccodecow=230 if country==10
replace ccodecow=375 if country==11
replace ccodecow=220 if country==12
replace ccodecow=200 if country==13
replace ccodecow=350 if country==14
replace ccodecow=310 if country==16
replace ccodecow=205 if country==17
replace ccodecow=395 if country==19
replace ccodecow=325 if country==20
replace ccodecow=212 if country==22
replace ccodecow=210 if country==23
replace ccodecow=385 if country==24
replace ccodecow=290 if country==25
replace ccodecow=235 if country==26
replace ccodecow=380 if country==28
replace ccodecow=317 if country==30
replace ccodecow=640 if country==31



gen year =.


foreach x of num 2002(1)2006{
	replace year = `x' if inwyr==`x'
	
}


foreach x of num 2006(1)2017{
	replace year = `x' if inwyys==`x'
	
}





merge m:m cntryID year using impic2016.dta

*keep if brncntr==2

save ESS_IMPIC_migrants.dta, replace

capture drop _merge
merge m:m ccodecow year using qog_std_ts_jan20.dta,  keepusing(oecd_socexpnd_t1a wdi_gdppppcon2011 wdi_unempilo wdi_chexppgdp wdi_expedu)


capture drop _merge
merge m:m ccodecow essround using qog_std_ts_jan20.dta,  keepusing(expenditure_current gdp_current unempl_current vi_wcoord vi_udr)



rename oecd_socexpnd_t1a expenditure
rename wdi_gdppppcon2011 gdp
rename wdi_unempilo unemp_rate
rename wdi_chexppgdp health_expend
rename wdi_expedu education_expend

/*
oecd_s~d_t1a /*public social expenditure*/
wdi_gdpppp~2011 /*GDP, PPP (constant 2011 international $)*/
wdi_unempilo /*unemployment*/
*/


save ESS_IMPIC_migrants.dta, replace


*Merge EPL data from OECD


capture drop _merge
merge m:m country essround using EPL_OECD.dta,  keepusing(epl)

save ESS_IMPIC_migrants.dta, replace


*Recoding variables: economic integration

gen income=hinctnt
replace income=hinctnta if essround>3 /*income*/

gen income_log=log(income)

tab uemp3m
gen unemp_3m =.
replace unemp_3m =1 if uemp3m==1
replace unemp_3m =0 if uemp3m==2 /*ever unemployed for more than 3 months*/

gen permanent=.
replace permanent=1 if wrkctr==1 & essround==1
replace permanent=0 if wrkctr==2 & essround==1
replace permanent=1 if wrkctra==1 & essround==1 & country==16
replace permanent=0 if wrkctra>1 & essround==1 & country==16
replace permanent=1 if wrkctra==1 & essround>1 & country!=16
replace permanent=0 if wrkctra>1 & essround>1 & country!=16 /*temporary contract*/

rename uempla unemp_now /*unemployed now*/

gen active=mnactic==1 /*active*/


*Recoding variables: political support / integration

factor trstprl trstlgl trstplc trstplt trstprt /*two factors among migrants: politicians and legal system*/
rotate, varimax


gen trust_politicians=trstprl+trstplt+trstprt
gen trust_legal=trstlgl+trstplc



tab1 stfdem /*democratic satisfaction*/

tab1 mmbprty wrkprty contplt wrkorg /*conventional participation*/
foreach x in mmbprty wrkprty contplt wrkorg {
recode `x' 2=0 1=1 
}

gen conv_part=mmbprty+wrkprty+contplt+wrkorg 

tab1 badge sgnptit pbldmn bctprd /*unconventional participation*/

foreach x in badge sgnptit pbldmn bctprd {
recode `x' 2=0 1=1 
}

gen unconv_part= badge+sgnptit+pbldmn+bctprd


gen close_party=clsprty==1

gen vote_national=vote==1


factor nwsppol rdpol tvpol /* media news consumption belong to a single factor. But unclear whether host or origin country media so let's not use this*/
rotate, varimax

gen media = nwsppol+rdpol+tvpol

*Social integration


tab ppltrst /*interpersonal trust*/

tab happy /*happiness*/

tab sclmeet /*frequency meeting with friends, relatives, colleagues*/

tab sclact /*frequency taking part in social activities*/


tab1 dscrgrp dscrrce dscrntn dscretn /* feeling disciminated*/

gen discrimination=dscretn+dscrntn+dscrrce /*discriminated by ethnic group, nationality, race*/


*Other variables

encode cntbrth, gen(cntbrth_num)
encode cntbrtha, gen(cntbrtha_num)
encode cntbrthb, gen(cntbrthb_num) 
encode cntbrthc, gen(cntbrthc_num)

tab1 cntbrthb_num cntbrthc_num /*let's do rounds 5-8 only*/
numlabel cntbrthb_num, add  
numlabel cntbrthc_num, add


*EU + Norway and Switzerland

gen eu=.
replace eu=0 if cntbrthb_num==1 /*USSR*/
replace eu=0 if cntbrthb_num==2 /*Czechoslovakia*/
replace eu=0 if cntbrthb_num==3 /*Yugoslavia*/
replace eu=0 if cntbrthb_num==4 /*Serbia and Montenegro*/
replace eu=0 if cntbrthb_num==8 /*Afghanistan*/
replace eu=0 if cntbrthb_num==9 /*Albania*/
replace eu=0 if cntbrthb_num==10 /*Armenia*/
replace eu=0 if cntbrthb_num==11 /*Netherlands Antilles*/
replace eu=0 if cntbrthb_num==12 /*Angola*/
replace eu=0 if cntbrthb_num==13 /*Argentina*/
replace eu=1 if cntbrthb_num==14 /*Austria*/
replace eu=0 if cntbrthb_num==14 & year<1995 /*Austria before accession*/
replace eu=0 if cntbrthb_num==15 /*Australia*/
replace eu=0 if cntbrthb_num==16 /*Aruba*/
replace eu=0 if cntbrthb_num==17 /*Aland Islands*/
replace eu=0 if cntbrthb_num==18 /*Azerbaijan*/
replace eu=0 if cntbrthb_num==19 /*Bosnia and Herzegovina*/
replace eu=0 if cntbrthb_num==20 /*Bangladesh*/
replace eu=1 if cntbrthb_num==21 /*Belgium*/
replace eu=0 if cntbrthb_num==22 /*Burkina Faso*/
replace eu=1 if cntbrthb_num==23 /*Bulgaria*/
replace eu=0 if cntbrthb_num==23 & year<2004 /*Bulgaria before accession*/
replace eu=0 if cntbrthb_num==24 /*Burundi*/
replace eu=0 if cntbrthb_num==25 /*Benin*/
replace eu=0 if cntbrthb_num==26 /*Brunei Darussalam*/
replace eu=0 if cntbrthb_num==27 /*Bolivia*/
replace eu=0 if cntbrthb_num==28 /*Brazil*/
replace eu=0 if cntbrthb_num==29 /*Botswana*/
replace eu=0 if cntbrthb_num==30 /*Belarus*/
replace eu=0 if cntbrthb_num==31 /*Canada*/
replace eu=0 if cntbrthb_num==32 /*Democratic Republic Congo*/
replace eu=0 if cntbrthb_num==33 /*Central African Republic*/
replace eu=0 if cntbrthb_num==34 /*Congo*/
replace eu=1 if cntbrthb_num==35 /*Switzerland: not EU but we consider so due to similar arrangements*/
replace eu=0 if cntbrthb_num==36 /*Cote Ivoire*/
replace eu=0 if cntbrthb_num==37 /*Chile*/
replace eu=0 if cntbrthb_num==38 /*Cameroon*/
replace eu=0 if cntbrthb_num==39 /*China*/
replace eu=0 if cntbrthb_num==40 /*Colombia*/
replace eu=0 if cntbrthb_num==41 /*Cuba*/
replace eu=0 if cntbrthb_num==42 /*Cape Verde*/
replace eu=1 if cntbrthb_num==43 /*Cyprus*/
replace eu=0 if cntbrthb_num==43 & year<2004 /*Cyprus*/
replace eu=1 if cntbrthb_num==44 /*Czech Republic*/
replace eu=0 if cntbrthb_num==44 & year<2004 /*Czech Republic*/
replace eu=1 if cntbrthb_num==45 /*Germany*/
replace eu=1 if cntbrthb_num==46 /*Denmark*/
replace eu=0 if cntbrthb_num==46 & year<1973 /*Denmark*/
replace eu=0 if cntbrthb_num==47 /*Dominican Republic*/
replace eu=0 if cntbrthb_num==48 /*Algeria*/
replace eu=0 if cntbrthb_num==49 /*Ecuador*/
replace eu=1 if cntbrthb_num==50 /*Estonia*/
replace eu=0 if cntbrthb_num==50 & year<2004 /*Estonia*/
replace eu=0 if cntbrthb_num==51 /*Egypt*/
replace eu=0 if cntbrthb_num==52 /*Eritrea*/
replace eu=1 if cntbrthb_num==53 /*Spain*/
replace eu=0 if cntbrthb_num==53 & year<1986 /*Spain*/
replace eu=0 if cntbrthb_num==54 /*Ethiopia*/
replace eu=1 if cntbrthb_num==55 /*Finland*/
replace eu=0 if cntbrthb_num==55 & year<1995 /*Finland*/
replace eu=0 if cntbrthb_num==56 /*Faroe Islands*/
replace eu=1 if cntbrthb_num==57 /*France*/
replace eu=0 if cntbrthb_num==58 /*Gabon*/
replace eu=1 if cntbrthb_num==59 /*Great Britain*/
replace eu=0 if cntbrthb_num==59 & year < 1973/*Great Britain*/
replace eu=0 if cntbrthb_num==60 /*Georgia*/
replace eu=0 if cntbrthb_num==61 /*Ghana*/
replace eu=0 if cntbrthb_num==62 /*Greenland*/
replace eu=0 if cntbrthb_num==63 /*Gambia*/
replace eu=0 if cntbrthb_num==64 /*Guinea*/
replace eu=0 if cntbrthb_num==65 /*Guadelupe*/
replace eu=1 if cntbrthb_num==66 /*Greece*/
replace eu=0 if cntbrthb_num==66 & year<1981 /*Greece*/
replace eu=0 if cntbrthb_num==67 /*Guatemala*/
replace eu=0 if cntbrthb_num==68 /*Guinea-Bissau*/
replace eu=0 if cntbrthb_num==69 /*Guyana*/
replace eu=0 if cntbrthb_num==70 /*Hong Kong*/
replace eu=0 if cntbrthb_num==71 /*Honduras*/
replace eu=1 if cntbrthb_num==72 /*Croatia*/
replace eu=0 if cntbrthb_num==72 & year<2004 /*Croatia*/
replace eu=1 if cntbrthb_num==73 /*Hungary*/
replace eu=0 if cntbrthb_num==73 & year<2004 /*Hungary*/
replace eu=0 if cntbrthb_num==74 /*Indonesia*/
replace eu=1 if cntbrthb_num==75 /*Ireland*/
replace eu=0 if cntbrthb_num==75 & year<1973 /*Ireland*/
replace eu=0 if cntbrthb_num==76 /*Israel*/
replace eu=0 if cntbrthb_num==77 /*India*/
replace eu=0 if cntbrthb_num==78 /*Iraq*/
replace eu=0 if cntbrthb_num==79 /*Iran*/
replace eu=0 if cntbrthb_num==80 /*Iceland*/
replace eu=1 if cntbrthb_num==81 /*Italy*/
replace eu=1 if cntbrthb_num==82 /*Jersey (UK dependency)*/
replace eu=0 if cntbrthb_num==82 & year<1973 /*Jersey (UK dependency)*/
replace eu=0 if cntbrthb_num==83 /*Jamaica*/
replace eu=0 if cntbrthb_num==84 /*Jordan*/
replace eu=0 if cntbrthb_num==85 /*Japan*/
replace eu=0 if cntbrthb_num==86 /*Kenya*/
replace eu=0 if cntbrthb_num==87 /*Kyrgyzstan*/
replace eu=0 if cntbrthb_num==88 /*Cambodia*/
replace eu=0 if cntbrthb_num==89 /*Comoros*/
replace eu=0 if cntbrthb_num==90 /*Korea, Democratic People's Republic of*/
replace eu=0 if cntbrthb_num==91 /*Korea, Republic of*/
replace eu=0 if cntbrthb_num==92 /*Kuwait*/
replace eu=0 if cntbrthb_num==93 /*Kazakhstan*/
replace eu=0 if cntbrthb_num==94 /*Lao People's Democratic Republic*/
replace eu=0 if cntbrthb_num==95 /*Lebanon*/
replace eu=0 if cntbrthb_num==96 /*Saint Lucia*/
replace eu=0 if cntbrthb_num==97 /*Liechtenstein*/
replace eu=0 if cntbrthb_num==98 /*Sri Lanka*/
replace eu=0 if cntbrthb_num==99 /*Liberia*/
replace eu=1 if cntbrthb_num==100 /*Lithuania*/
replace eu=0 if cntbrthb_num==100 & year<2004 /*Lithuania*/
replace eu=1 if cntbrthb_num==101 /*Luxembourg*/
replace eu=1 if cntbrthb_num==102 /*Latvia*/
replace eu=0 if cntbrthb_num==102 & year<2004 /*Latvia*/
replace eu=0 if cntbrthb_num==103 /*Libyan Arab Jamahiriya*/
replace eu=0 if cntbrthb_num==104 /*Morocco*/
replace eu=0 if cntbrthb_num==105 /*Moldova*/
replace eu=0 if cntbrthb_num==106 /*Montenegro*/
replace eu=0 if cntbrthb_num==107 /*Madagascar*/
replace eu=0 if cntbrthb_num==108 /*Macedonia*/
replace eu=0 if cntbrthb_num==109 /*Mali*/
replace eu=0 if cntbrthb_num==110 /*Macao*/
replace eu=0 if cntbrthb_num==111 /*Martinique*/
replace eu=0 if cntbrthb_num==112 /*Montserrat*/
replace eu=1 if cntbrthb_num==113 /*Malta*/
replace eu=0 if cntbrthb_num==113 & year<2004 /*Malta*/
replace eu=0 if cntbrthb_num==114 /*Mauritius*/
replace eu=0 if cntbrthb_num==115 /*Mexico*/
replace eu=0 if cntbrthb_num==116 /*Malaysia*/
replace eu=0 if cntbrthb_num==117 /*Mozambique*/
replace eu=0 if cntbrthb_num==118 /*Namibia*/
replace eu=0 if cntbrthb_num==119 /*Nigeria*/
replace eu=0 if cntbrthb_num==120 /*Nicaragua*/
replace eu=1 if cntbrthb_num==121 /*Netherlands*/
replace eu=1 if cntbrthb_num==122 /*Norway*/
replace eu=0 if cntbrthb_num==123 /*Nepal*/
replace eu=0 if cntbrthb_num==124 /*New Zealand*/
replace eu=0 if cntbrthb_num==125 /*Panama*/
replace eu=0 if cntbrthb_num==126 /*Peru*/
replace eu=0 if cntbrthb_num==127 /*Philipines*/
replace eu=0 if cntbrthb_num==128 /*Pakistan*/
replace eu=1 if cntbrthb_num==129 /*Poland*/
replace eu=0 if cntbrthb_num==129 & year<2004 /*Poland*/
replace eu=0 if cntbrthb_num==130 /*Palestina*/
replace eu=1 if cntbrthb_num==131 /*Portugal*/
replace eu=0 if cntbrthb_num==131 & year<1986 /*Portugal*/
replace eu=0 if cntbrthb_num==132 /*Paraguay*/
replace eu=0 if cntbrthb_num==133 /*Reunion*/
replace eu=1 if cntbrthb_num==134 /*Romania*/
replace eu=0 if cntbrthb_num==134 & year<2004 /*Romania*/
replace eu=0 if cntbrthb_num==135 /*Serbia*/
replace eu=0 if cntbrthb_num==136 /*Russia*/
replace eu=0 if cntbrthb_num==137 /*Rwanda*/
replace eu=0 if cntbrthb_num==138 /*Saudi Arabia*/
replace eu=0 if cntbrthb_num==139 /*Sudan*/
replace eu=1 if cntbrthb_num==140 /*Sweden*/
replace eu=0 if cntbrthb_num==140 & year<1995 /*Sweden*/
replace eu=0 if cntbrthb_num==141 /*Singapore*/
replace eu=1 if cntbrthb_num==142 /*Slovenia*/
replace eu=0 if cntbrthb_num==142 & year<2004 /*Slovenia*/
replace eu=1 if cntbrthb_num==143 /*Slovakia*/
replace eu=0 if cntbrthb_num==143 & year<2004 /*Slovakia*/
replace eu=0 if cntbrthb_num==144 /*Sierra Leone*/
replace eu=0 if cntbrthb_num==145 /*Senegal*/
replace eu=0 if cntbrthb_num==146 /*Somalia*/
replace eu=0 if cntbrthb_num==147 /*Suriname*/
replace eu=0 if cntbrthb_num==148 /*Sao Tome and Principe*/
replace eu=0 if cntbrthb_num==149 /*El Salvador*/
replace eu=0 if cntbrthb_num==150 /*Syria*/
replace eu=0 if cntbrthb_num==151 /*Togo*/
replace eu=0 if cntbrthb_num==152 /*Thailand*/
replace eu=0 if cntbrthb_num==153 /*Tajikistan*/
replace eu=0 if cntbrthb_num==154 /*Tunisia*/
replace eu=0 if cntbrthb_num==155 /*Turkey*/
replace eu=0 if cntbrthb_num==156 /*Trinidad and Tobago*/
replace eu=0 if cntbrthb_num==157 /*Taiwan*/
replace eu=0 if cntbrthb_num==158 /*Tanzania*/
replace eu=0 if cntbrthb_num==159 /*Ukraine*/
replace eu=0 if cntbrthb_num==160 /*Uganda*/
replace eu=0 if cntbrthb_num==161 /*United States*/
replace eu=0 if cntbrthb_num==162 /*Uruguay*/
replace eu=0 if cntbrthb_num==163 /*Uzbekistan*/
replace eu=0 if cntbrthb_num==164 /*Venezuela*/
replace eu=0 if cntbrthb_num==165 /*Vietnam*/
replace eu=0 if cntbrthb_num==166 /*Yemen*/
replace eu=0 if cntbrthb_num==167 /*South Africa*/
replace eu=0 if cntbrthb_num==168 /*Zambia*/
replace eu=0 if cntbrthb_num==169 /*Zimbabwe*/

replace eu=0 if cntbrthc_num==1 /*DDR*/
replace eu=0 if cntbrthc_num==2 /*USSR*/
replace eu=0 if cntbrthc_num==3 /*Czechoslovakia*/
replace eu=0 if cntbrthc_num==4 /*Yugoslavia*/
replace eu=0 if cntbrthc_num==5 /*Serbia and Montenegro*/
replace eu=0 if cntbrthc_num==9 /*United Arab Emirates*/
replace eu=0 if cntbrthc_num==10 /*Afghanistan*/
replace eu=0 if cntbrthc_num==11 /*Albania*/
replace eu=0 if cntbrthc_num==12 /*Armenia*/
replace eu=0 if cntbrthc_num==13 /*Angola*/
replace eu=0 if cntbrthc_num==14 /*Argentina*/
replace eu=1 if cntbrthc_num==15 /*Austria*/
replace eu=0 if cntbrthc_num==15 & year<1995 /*Austria before accession*/
replace eu=0 if cntbrthc_num==16 /*Australia*/
replace eu=0 if cntbrthc_num==17 /*Aruba*/
replace eu=0 if cntbrthc_num==18 /*Azerbajan*/
replace eu=0 if cntbrthc_num==19 /*Bosnia and Herzegovina*/
replace eu=0 if cntbrthc_num==20 /*Bangladesh*/
replace eu=1 if cntbrthc_num==21 /*Belgium*/
replace eu=0 if cntbrthc_num==22 /*Burkina Faso*/
replace eu=1 if cntbrthc_num==23 /*Bulgaria*/
replace eu=0 if cntbrthc_num==23 & year<2004 /*Bulgaria*/
replace eu=0 if cntbrthc_num==24 /*Burundi*/
replace eu=0 if cntbrthc_num==25 /*Benin*/
replace eu=0 if cntbrthc_num==26 /*Brunei Darussalam*/
replace eu=0 if cntbrthc_num==27 /*Bolivia*/
replace eu=0 if cntbrthc_num==28 /*Bonaire, Sint Eustatius and Saba*/
replace eu=0 if cntbrthc_num==29 /*Brazil*/
replace eu=0 if cntbrthc_num==30 /*Belarus*/
replace eu=0 if cntbrthc_num==31 /*Canada*/
replace eu=0 if cntbrthc_num==32 /*Congo, The Democratic Republic of the*/
replace eu=0 if cntbrthc_num==33 /*Central African Republic*/
replace eu=0 if cntbrthc_num==34 /*Congo*/
replace eu=1 if cntbrthc_num==35 /*Switzerland*/
replace eu=0 if cntbrthc_num==36 /*Cote Ivoire*/
replace eu=0 if cntbrthc_num==37 /*Chile*/
replace eu=0 if cntbrthc_num==38 /*Cameroon*/
replace eu=0 if cntbrthc_num==39 /*China*/
replace eu=0 if cntbrthc_num==40 /*Colombia*/
replace eu=0 if cntbrthc_num==41 /*Costa Rica*/
replace eu=0 if cntbrthc_num==42 /*Cuba*/
replace eu=0 if cntbrthc_num==43 /*Cape Verde*/
replace eu=0 if cntbrthc_num==44 /*Curacao*/
replace eu=1 if cntbrthc_num==45 /*Czech republic*/
replace eu=0 if cntbrthc_num==45 & year<2004 /*Czech republic*/
replace eu=1 if cntbrthc_num==46 /*Germany*/
replace eu=0 if cntbrthc_num==47 /*Djibouti*/
replace eu=1 if cntbrthc_num==48 /*Denmark*/
replace eu=0 if cntbrthc_num==48 & year<1973/*Denmark*/
replace eu=0 if cntbrthc_num==49 /*Dominica*/
replace eu=0 if cntbrthc_num==50 /*Dominican Republic*/
replace eu=0 if cntbrthc_num==51 /*Algeria*/
replace eu=0 if cntbrthc_num==52 /*Ecuador*/
replace eu=1 if cntbrthc_num==53 /*Estonia*/
replace eu=0 if cntbrthc_num==53 & year<2004 /*Estonia*/
replace eu=0 if cntbrthc_num==54 /*Egypt*/
replace eu=0 if cntbrthc_num==55 /*Eritrea*/
replace eu=1 if cntbrthc_num==56 /*Spain*/
replace eu=0 if cntbrthc_num==56 & year<1986 /*Spain*/
replace eu=0 if cntbrthc_num==57 /*Ethiopia*/
replace eu=1 if cntbrthc_num==58 /*Finland*/
replace eu=0 if cntbrthc_num==58 & year<1995 /*Finland*/
replace eu=0 if cntbrthc_num==59 /*Faroe Islands*/
replace eu=1 if cntbrthc_num==60 /*France*/
replace eu=0 if cntbrthc_num==61 /*Gabon*/
replace eu=1 if cntbrthc_num==62 /*Great Britain*/
replace eu=0 if cntbrthc_num==62 & year<1973 /*Great Britain*/
replace eu=0 if cntbrthc_num==63 /*Grenada*/
replace eu=0 if cntbrthc_num==64 /*Georgia*/
replace eu=0 if cntbrthc_num==65 /*French Guinea*/
replace eu=0 if cntbrthc_num==66 /*Ghana*/
replace eu=0 if cntbrthc_num==67 /*Greenland*/
replace eu=0 if cntbrthc_num==68 /*Gambia*/
replace eu=0 if cntbrthc_num==69 /*Guinea*/
replace eu=0 if cntbrthc_num==70 /*Guadalupe*/
replace eu=0 if cntbrthc_num==71 /*Equatorial Guinea*/
replace eu=1 if cntbrthc_num==72 /*Greece*/
replace eu=0 if cntbrthc_num==72 & year<1981 /*Greece*/
replace eu=0 if cntbrthc_num==73 /*Guatemala*/
replace eu=0 if cntbrthc_num==74 /*Guinea Bissau*/
replace eu=0 if cntbrthc_num==75 /*Guyana*/
replace eu=0 if cntbrthc_num==76 /*Hong Kong*/
replace eu=0 if cntbrthc_num==77 /*Honduras*/
replace eu=1 if cntbrthc_num==78 /*Croatia*/
replace eu=0 if cntbrthc_num==78 & year<2004 /*Croatia*/
replace eu=1 if cntbrthc_num==79 /*Hungary*/
replace eu=0 if cntbrthc_num==79 & year<2004 /*Hungary*/
replace eu=0 if cntbrthc_num==80 /*Indonesia*/
replace eu=1 if cntbrthc_num==81 /*Ireland*/
replace eu=0 if cntbrthc_num==81 & year<1973 /*Ireland*/
replace eu=0 if cntbrthc_num==82 /*Israel*/
replace eu=0 if cntbrthc_num==83 /*India*/
replace eu=0 if cntbrthc_num==84 /*Iraq*/
replace eu=0 if cntbrthc_num==85 /*Iran*/
replace eu=0 if cntbrthc_num==86 /*Iceland*/
replace eu=1 if cntbrthc_num==87 /*Italy*/
replace eu=1 if cntbrthc_num==88 /*Jersey (UK dependency)*/
replace eu=0 if cntbrthc_num==88 & year<1973 /*Jersey (UK dependency)*/
replace eu=0 if cntbrthc_num==89 /*Jamaica*/
replace eu=0 if cntbrthc_num==90 /*Jordan*/
replace eu=0 if cntbrthc_num==91 /*Japan*/
replace eu=0 if cntbrthc_num==92 /*Kenya*/
replace eu=0 if cntbrthc_num==93 /*Kyrgyzstan*/
replace eu=0 if cntbrthc_num==94 /*Cambodia*/
replace eu=0 if cntbrthc_num==95 /*Comoros*/
replace eu=0 if cntbrthc_num==96 /*Korea, Republic of*/
replace eu=0 if cntbrthc_num==97 /*Kuwait*/
replace eu=0 if cntbrthc_num==98 /*Kazakhstan*/
replace eu=0 if cntbrthc_num==99 /*Lao People's Democratic Republic*/
replace eu=0 if cntbrthc_num==100 /*Lebanon*/
replace eu=0 if cntbrthc_num==101 /*Saint Lucia*/
replace eu=0 if cntbrthc_num==102 /*Sri Lanka*/
replace eu=0 if cntbrthc_num==103 /*Liberia*/
replace eu=0 if cntbrthc_num==104 /*Lesotho*/
replace eu=1 if cntbrthc_num==105 /*Lithuania*/
replace eu=0 if cntbrthc_num==105 & year<2004 /*Lithuania*/
replace eu=1 if cntbrthc_num==106 /*Luxembourg*/
replace eu=1 if cntbrthc_num==107 /*Latvia*/
replace eu=0 if cntbrthc_num==107 & year<2004 /*Latvia*/
replace eu=0 if cntbrthc_num==108 /*Lybia*/
replace eu=0 if cntbrthc_num==109 /*Morocco*/
replace eu=0 if cntbrthc_num==110 /*Moldova, Republic of*/
replace eu=0 if cntbrthc_num==111 /*Madagascar*/
replace eu=0 if cntbrthc_num==112 /*Macedonia, The Former Yugoslav Republic of*/
replace eu=0 if cntbrthc_num==113 /*Mali*/
replace eu=0 if cntbrthc_num==114 /*Mongolia*/
replace eu=0 if cntbrthc_num==115 /*Martinique*/
replace eu=0 if cntbrthc_num==116 /*Mauritania*/
replace eu=1 if cntbrthc_num==117 /*Malta*/
replace eu=0 if cntbrthc_num==117 & year<2004 /*Malta*/
replace eu=0 if cntbrthc_num==118 /*Mauritius*/
replace eu=0 if cntbrthc_num==119 /*Malawi*/
replace eu=0 if cntbrthc_num==120 /*Mexico*/
replace eu=0 if cntbrthc_num==121 /*Malaysia*/
replace eu=0 if cntbrthc_num==122 /*Mozambique*/
replace eu=0 if cntbrthc_num==123 /*Niger*/
replace eu=0 if cntbrthc_num==124 /*Nigeria*/
replace eu=0 if cntbrthc_num==125 /*Nicaragua*/
replace eu=1 if cntbrthc_num==126 /*Netherlands*/
replace eu=1 if cntbrthc_num==127 /*Norway*/
replace eu=0 if cntbrthc_num==128 /*Nepal*/
replace eu=0 if cntbrthc_num==129 /*New Zealand*/
replace eu=0 if cntbrthc_num==130 /*Panama*/
replace eu=0 if cntbrthc_num==131 /*Peru*/
replace eu=0 if cntbrthc_num==132 /*French Polynesia*/
replace eu=0 if cntbrthc_num==133 /*Philipnnes*/
replace eu=0 if cntbrthc_num==134 /*Pakistan*/
replace eu=1 if cntbrthc_num==135 /*Poland*/
replace eu=0 if cntbrthc_num==135 & year<2004 /*Poland*/
replace eu=0 if cntbrthc_num==136 /*Puerto Rico*/
replace eu=0 if cntbrthc_num==137 /*Palestine*/
replace eu=1 if cntbrthc_num==138 /*Portugal*/
replace eu=0 if cntbrthc_num==138 & year<1986 /*Portugal*/
replace eu=0 if cntbrthc_num==139 /*Paraguay*/
replace eu=0 if cntbrthc_num==140 /*Reunion*/
replace eu=1 if cntbrthc_num==141 /*Romania*/
replace eu=0 if cntbrthc_num==141 & year<2004 /*Romania*/
replace eu=0 if cntbrthc_num==142 /*Serbia*/
replace eu=0 if cntbrthc_num==143 /*Russia*/
replace eu=0 if cntbrthc_num==144 /*Rwanda*/
replace eu=0 if cntbrthc_num==145 /*Saudi Arabia*/
replace eu=0 if cntbrthc_num==146 /*Seychelles*/
replace eu=0 if cntbrthc_num==147 /*Sudan*/
replace eu=1 if cntbrthc_num==148 /*Sweden*/
replace eu=0 if cntbrthc_num==148 & year<1995 /*Sweden*/
replace eu=0 if cntbrthc_num==149 /*Singapore*/
replace eu=1 if cntbrthc_num==150 /*Slovenia*/
replace eu=0 if cntbrthc_num==150 & year<2004 /*Slovenia*/
replace eu=1 if cntbrthc_num==151 /*Slovakia*/
replace eu=0 if cntbrthc_num==151 & year<2004 /*Slovakia*/
replace eu=0 if cntbrthc_num==152 /*Sierra Leone*/
replace eu=0 if cntbrthc_num==153 /*Senegal*/
replace eu=0 if cntbrthc_num==154 /*Somalia*/
replace eu=0 if cntbrthc_num==155 /*Suriname*/
replace eu=0 if cntbrthc_num==156 /*Sao Tome and Principe*/
replace eu=0 if cntbrthc_num==157 /*El Salvador*/
replace eu=0 if cntbrthc_num==158 /*Sint Maarten (Dutch Part)*/
replace eu=0 if cntbrthc_num==159 /*Syrian*/
replace eu=0 if cntbrthc_num==160 /*Chad*/
replace eu=0 if cntbrthc_num==161 /*Togo*/
replace eu=0 if cntbrthc_num==162 /*Thailand*/
replace eu=0 if cntbrthc_num==163 /*Tajikistan*/
replace eu=0 if cntbrthc_num==164 /*Turkmenistan*/
replace eu=0 if cntbrthc_num==165 /*Tunisia*/
replace eu=0 if cntbrthc_num==166 /*Turkey*/
replace eu=0 if cntbrthc_num==167 /*Trinidad and Tobago*/
replace eu=0 if cntbrthc_num==168 /*Taiwan*/
replace eu=0 if cntbrthc_num==169 /*Tanzania*/
replace eu=0 if cntbrthc_num==170 /*Ukraine*/
replace eu=0 if cntbrthc_num==171 /*Uganda*/
replace eu=0 if cntbrthc_num==172 /*US*/
replace eu=0 if cntbrthc_num==173 /*Uruguay*/
replace eu=0 if cntbrthc_num==174 /*Uzbekistan*/
replace eu=0 if cntbrthc_num==175 /*Venezuela*/
replace eu=0 if cntbrthc_num==176 /*Vietnam*/
replace eu=0 if cntbrthc_num==177 /*Kosovo*/
replace eu=0 if cntbrthc_num==178 /*Mayotte*/
replace eu=0 if cntbrthc_num==179 /*South Africa*/
replace eu=0 if cntbrthc_num==180 /*Zambia*/
replace eu=0 if cntbrthc_num==181 /*Zimbabwe*/


*Country of origin variables

gen codecow2=.
replace codecow2=315 if cntbrthb_num==2 /*Czechoslovakia*/
replace codecow2=345 if cntbrthb_num==3 /*Yugoslavia*/
replace codecow2=345 if cntbrthb_num==4 /*Serbia and Montenegro*/
replace codecow2=700 if cntbrthb_num==8 /*Afghanistan*/
replace codecow2=339 if cntbrthb_num==9 /*Albania*/
replace codecow2=371 if cntbrthb_num==10 /*Armenia*/
replace codecow2=540 if cntbrthb_num==12 /*Angola*/
replace codecow2=160 if cntbrthb_num==13 /*Argentina*/
replace codecow2=305 if cntbrthb_num==14 /*Austria*/
replace codecow2=900 if cntbrthb_num==15 /*Australia*/
replace codecow2=373 if cntbrthb_num==18 /*Azerbaijan*/
replace codecow2=346 if cntbrthb_num==19 /*Bosnia and Herzegovina*/
replace codecow2=771 if cntbrthb_num==20 /*Bangladesh*/
replace codecow2=211 if cntbrthb_num==21 /*Belgium*/
replace codecow2=439 if cntbrthb_num==22 /*Burkina Faso*/
replace codecow2=355 if cntbrthb_num==23 /*Bulgaria*/
replace codecow2=516 if cntbrthb_num==24 /*Burundi*/
replace codecow2=434 if cntbrthb_num==25 /*Benin*/
replace codecow2=835 if cntbrthb_num==26 /*Brunei Darussalam*/
replace codecow2=145 if cntbrthb_num==27 /*Bolivia*/
replace codecow2=140 if cntbrthb_num==28 /*Brazil*/
replace codecow2=571 if cntbrthb_num==29 /*Botswana*/
replace codecow2=370 if cntbrthb_num==30 /*Belarus*/
replace codecow2=20 if cntbrthb_num==31 /*Canada*/
replace codecow2=490 if cntbrthb_num==32 /*Democratic Republic Congo*/
replace codecow2=482 if cntbrthb_num==33 /*Central African Republic*/
replace codecow2=484 if cntbrthb_num==34 /*Congo*/
replace codecow2=225 if cntbrthb_num==35 /*Switzerland: not codecow2 but we consider so due to similar arrangements*/
replace codecow2=437 if cntbrthb_num==36 /*Cote Ivoire*/
replace codecow2=155 if cntbrthb_num==37 /*Chile*/
replace codecow2=471 if cntbrthb_num==38 /*Cameroon*/
replace codecow2=710 if cntbrthb_num==39 /*China*/
replace codecow2=100 if cntbrthb_num==40 /*Colombia*/
replace codecow2=40 if cntbrthb_num==41 /*Cuba*/
replace codecow2=402 if cntbrthb_num==42 /*Cape Verde*/
replace codecow2=352 if cntbrthb_num==43 /*Cyprus*/
replace codecow2=316 if cntbrthb_num==44 /*Czech Republic*/
replace codecow2=255 if cntbrthb_num==45 /*Germany*/
replace codecow2=390 if cntbrthb_num==46 /*Denmark*/
replace codecow2=42 if cntbrthb_num==47 /*Dominican Republic*/
replace codecow2=615 if cntbrthb_num==48 /*Algeria*/
replace codecow2=130 if cntbrthb_num==49 /*Ecuador*/
replace codecow2=366 if cntbrthb_num==50 /*Estonia*/
replace codecow2=651 if cntbrthb_num==51 /*Egypt*/
replace codecow2=531 if cntbrthb_num==52 /*Eritrea*/
replace codecow2=230 if cntbrthb_num==53 /*Spain*/
replace codecow2=530 if cntbrthb_num==54 /*Ethiopia*/
replace codecow2=375 if cntbrthb_num==55 /*Finland*/
replace codecow2=220 if cntbrthb_num==57 /*France*/
replace codecow2=481 if cntbrthb_num==58 /*Gabon*/
replace codecow2=200 if cntbrthb_num==59 /*Great Britain*/
replace codecow2=372 if cntbrthb_num==60 /*Georgia*/
replace codecow2=452 if cntbrthb_num==61 /*Ghana*/
replace codecow2=420 if cntbrthb_num==63 /*Gambia*/
replace codecow2=438 if cntbrthb_num==64 /*Guinea*/
replace codecow2=350 if cntbrthb_num==66 /*Greece*/
replace codecow2=90 if cntbrthb_num==67 /*Guatemala*/
replace codecow2=404 if cntbrthb_num==68 /*Guinea-Bissau*/
replace codecow2=110 if cntbrthb_num==69 /*Guyana*/
replace codecow2=997 if cntbrthb_num==70 /*Hong Kong*/
replace codecow2=91 if cntbrthb_num==71 /*Honduras*/
replace codecow2=344 if cntbrthb_num==72 /*Croatia*/
replace codecow2=310 if cntbrthb_num==73 /*Hungary*/
replace codecow2=850 if cntbrthb_num==74 /*Indonesia*/
replace codecow2=205 if cntbrthb_num==75 /*Ireland*/
replace codecow2=666 if cntbrthb_num==76 /*Israel*/
replace codecow2=750 if cntbrthb_num==77 /*India*/
replace codecow2=645 if cntbrthb_num==78 /*Iraq*/
replace codecow2=630 if cntbrthb_num==79 /*Iran*/
replace codecow2=395 if cntbrthb_num==80 /*Iceland*/
replace codecow2=325 if cntbrthb_num==81 /*Italy*/
replace codecow2=51 if cntbrthb_num==83 /*Jamaica*/
replace codecow2=663 if cntbrthb_num==84 /*Jordan*/
replace codecow2=740 if cntbrthb_num==85 /*Japan*/
replace codecow2=501 if cntbrthb_num==86 /*Kenya*/
replace codecow2=703 if cntbrthb_num==87 /*Kyrgyzstan*/
replace codecow2=811 if cntbrthb_num==88 /*Cambodia*/
replace codecow2=581 if cntbrthb_num==89 /*Comoros*/
replace codecow2=731 if cntbrthb_num==90 /*Korea, Democratic People's Republic of*/
replace codecow2=732 if cntbrthb_num==91 /*Korea, Republic of*/
replace codecow2=690 if cntbrthb_num==92 /*Kuwait*/
replace codecow2=705 if cntbrthb_num==93 /*Kazakhstan*/
replace codecow2=812 if cntbrthb_num==94 /*Lao People's Democratic Republic*/
replace codecow2=660 if cntbrthb_num==95 /*Lebanon*/
replace codecow2=56 if cntbrthb_num==96 /*Saint Lucia*/
replace codecow2=223 if cntbrthb_num==97 /*Liechtenstein*/
replace codecow2=780 if cntbrthb_num==98 /*Sri Lanka*/
replace codecow2=450 if cntbrthb_num==99 /*Liberia*/
replace codecow2=368 if cntbrthb_num==100 /*Lithuania*/
replace codecow2=212 if cntbrthb_num==101 /*Luxembourg*/
replace codecow2=367 if cntbrthb_num==102 /*Latvia*/
replace codecow2=620 if cntbrthb_num==103 /*Libyan Arab Jamahiriya*/
replace codecow2=600 if cntbrthb_num==104 /*Morocco*/
replace codecow2=359 if cntbrthb_num==105 /*Moldova*/
replace codecow2=341 if cntbrthb_num==106 /*Montenegro*/
replace codecow2=580 if cntbrthb_num==107 /*Madagascar*/
replace codecow2=343 if cntbrthb_num==108 /*Macedonia*/
replace codecow2=432 if cntbrthb_num==109 /*Mali*/
replace codecow2=710 if cntbrthb_num==110 /*Macao*/
replace codecow2=338 if cntbrthb_num==113 /*Malta*/
replace codecow2=590 if cntbrthb_num==114 /*Mauritius*/
replace codecow2=70 if cntbrthb_num==115 /*Mexico*/
replace codecow2=820 if cntbrthb_num==116 /*Malaysia*/
replace codecow2=541 if cntbrthb_num==117 /*Mozambique*/
replace codecow2=565 if cntbrthb_num==118 /*Namibia*/
replace codecow2=475 if cntbrthb_num==119 /*Nigeria*/
replace codecow2=93 if cntbrthb_num==120 /*Nicaragua*/
replace codecow2=210 if cntbrthb_num==121 /*Netherlands*/
replace codecow2=385 if cntbrthb_num==122 /*Norway*/
replace codecow2=790 if cntbrthb_num==123 /*Nepal*/
replace codecow2=920 if cntbrthb_num==124 /*New Zealand*/
replace codecow2=95 if cntbrthb_num==125 /*Panama*/
replace codecow2=135 if cntbrthb_num==126 /*Peru*/
replace codecow2=840 if cntbrthb_num==127 /*Philipines*/
replace codecow2=770 if cntbrthb_num==128 /*Pakistan*/
replace codecow2=290 if cntbrthb_num==129 /*Poland*/
replace codecow2=666 if cntbrthb_num==130 /*Palestina (Israel)*/
replace codecow2=235 if cntbrthb_num==131 /*Portugal*/
replace codecow2=150 if cntbrthb_num==132 /*Paraguay*/
replace codecow2=360 if cntbrthb_num==134 /*Romania*/
replace codecow2=345 if cntbrthb_num==135 /*Serbia*/
replace codecow2=365 if cntbrthb_num==136 /*Russia*/
replace codecow2=517 if cntbrthb_num==137 /*Rwanda*/
replace codecow2=670 if cntbrthb_num==138 /*Saudi Arabia*/
replace codecow2=625 if cntbrthb_num==139 /*Sudan*/
replace codecow2=380 if cntbrthb_num==140 /*Sweden*/
replace codecow2=830 if cntbrthb_num==141 /*Singapore*/
replace codecow2=349 if cntbrthb_num==142 /*Slovenia*/
replace codecow2=317 if cntbrthb_num==143 /*Slovakia*/
replace codecow2=451 if cntbrthb_num==144 /*Sierra Leone*/
replace codecow2=433 if cntbrthb_num==145 /*Senegal*/
replace codecow2=520 if cntbrthb_num==146 /*Somalia*/
replace codecow2=115 if cntbrthb_num==147 /*Suriname*/
replace codecow2=403 if cntbrthb_num==148 /*Sao Tome and Principe*/
replace codecow2=92 if cntbrthb_num==149 /*El Salvador*/
replace codecow2=652 if cntbrthb_num==150 /*Syria*/
replace codecow2=461 if cntbrthb_num==151 /*Togo*/
replace codecow2=800 if cntbrthb_num==152 /*Thailand*/
replace codecow2=702 if cntbrthb_num==153 /*Tajikistan*/
replace codecow2=616 if cntbrthb_num==154 /*Tunisia*/
replace codecow2=640 if cntbrthb_num==155 /*Turkey*/
replace codecow2=52 if cntbrthb_num==156 /*Trinidad and Tobago*/
replace codecow2=713 if cntbrthb_num==157 /*Taiwan*/
replace codecow2=510 if cntbrthb_num==158 /*Tanzania*/
replace codecow2=369 if cntbrthb_num==159 /*Ukraine*/
replace codecow2=500 if cntbrthb_num==160 /*Uganda*/
replace codecow2=2 if cntbrthb_num==161 /*United States*/
replace codecow2=165 if cntbrthb_num==162 /*Uruguay*/
replace codecow2=704 if cntbrthb_num==163 /*Uzbekistan*/
replace codecow2=101 if cntbrthb_num==164 /*Venezuela*/
replace codecow2=816 if cntbrthb_num==165 /*Vietnam*/
replace codecow2=679 if cntbrthb_num==166 /*Yemen*/
replace codecow2=560 if cntbrthb_num==167 /*South Africa*/
replace codecow2=551 if cntbrthb_num==168 /*Zambia*/
replace codecow2=552 if cntbrthb_num==169 /*Zimbabwe*/


replace codecow2=265 if cntbrthc_num==1 /*DDR*/
replace codecow2=315 if cntbrthc_num==3 /*Czechoslovakia*/
replace codecow2=345 if cntbrthc_num==4 /*Yugoslavia*/
replace codecow2=345 if cntbrthc_num==5 /*Serbia and Montenegro*/
replace codecow2=696 if cntbrthc_num==9 /*United Arab Emirates*/
replace codecow2=700 if cntbrthc_num==10 /*Afghanistan*/
replace codecow2=339 if cntbrthc_num==11 /*Albania*/
replace codecow2=371 if cntbrthc_num==12 /*Armenia*/
replace codecow2=540 if cntbrthc_num==13 /*Angola*/
replace codecow2=160 if cntbrthc_num==14 /*Argentina*/
replace codecow2=305 if cntbrthc_num==15 /*Austria*/
replace codecow2=900 if cntbrthc_num==16 /*Australia*/
replace codecow2=373 if cntbrthc_num==18 /*Azerbajan*/
replace codecow2=346 if cntbrthc_num==19 /*Bosnia and Herzegovina*/
replace codecow2=771 if cntbrthc_num==20 /*Bangladesh*/
replace codecow2=211 if cntbrthc_num==21 /*Belgium*/
replace codecow2=439 if cntbrthc_num==22 /*Burkina Faso*/
replace codecow2=355 if cntbrthc_num==23 /*Bulgaria*/
replace codecow2=516 if cntbrthc_num==24 /*Burundi*/
replace codecow2=434 if cntbrthc_num==25 /*Benin*/
replace codecow2=835 if cntbrthc_num==26 /*Brunei Darussalam*/
replace codecow2=145 if cntbrthc_num==27 /*Bolivia*/
replace codecow2=140 if cntbrthc_num==29 /*Brazil*/
replace codecow2=370 if cntbrthc_num==30 /*Belarus*/
replace codecow2=20 if cntbrthc_num==31 /*Canada*/
replace codecow2=490 if cntbrthc_num==32 /*Congo, The Democratic Republic of the*/
replace codecow2=482 if cntbrthc_num==33 /*Central African Republic*/
replace codecow2=484 if cntbrthc_num==34 /*Congo*/
replace codecow2=225 if cntbrthc_num==35 /*Switzerland*/
replace codecow2=437 if cntbrthc_num==36 /*Cote Ivoire*/
replace codecow2=155 if cntbrthc_num==37 /*Chile*/
replace codecow2=471 if cntbrthc_num==38 /*Cameroon*/
replace codecow2=710 if cntbrthc_num==39 /*China*/
replace codecow2=100 if cntbrthc_num==40 /*Colombia*/
replace codecow2=94 if cntbrthc_num==41 /*Costa Rica*/
replace codecow2=40 if cntbrthc_num==42 /*Cuba*/
replace codecow2=402 if cntbrthc_num==43 /*Cape Verde*/
replace codecow2=316 if cntbrthc_num==45 /*Czech republic*/
replace codecow2=255 if cntbrthc_num==46 /*Germany*/
replace codecow2=522 if cntbrthc_num==47 /*Djibouti*/
replace codecow2=390 if cntbrthc_num==48 /*Denmark*/
replace codecow2=54 if cntbrthc_num==49 /*Dominica*/
replace codecow2=42 if cntbrthc_num==50 /*Dominican Republic*/
replace codecow2=615 if cntbrthc_num==51 /*Algeria*/
replace codecow2=130 if cntbrthc_num==52 /*Ecuador*/
replace codecow2=366 if cntbrthc_num==53 /*Estonia*/
replace codecow2=651 if cntbrthc_num==54 /*Egypt*/
replace codecow2=531 if cntbrthc_num==55 /*Eritrea*/
replace codecow2=230 if cntbrthc_num==56 /*Spain*/
replace codecow2=530 if cntbrthc_num==57 /*Ethiopia*/
replace codecow2=375 if cntbrthc_num==58 /*Finland*/
replace codecow2=220 if cntbrthc_num==60 /*France*/
replace codecow2=481 if cntbrthc_num==61 /*Gabon*/
replace codecow2=200 if cntbrthc_num==62 /*Great Britain*/
replace codecow2=55 if cntbrthc_num==63 /*Grenada*/
replace codecow2=372 if cntbrthc_num==64 /*Georgia*/
replace codecow2=438 if cntbrthc_num==65 /*French Guinea*/
replace codecow2=452 if cntbrthc_num==66 /*Ghana*/
replace codecow2=420 if cntbrthc_num==68 /*Gambia*/
replace codecow2=438 if cntbrthc_num==69 /*Guinea*/
replace codecow2=411 if cntbrthc_num==71 /*Equatorial Guinea*/
replace codecow2=350 if cntbrthc_num==72 /*Greece*/
replace codecow2=90 if cntbrthc_num==73 /*Guatemala*/
replace codecow2=404 if cntbrthc_num==74 /*Guinea Bissau*/
replace codecow2=110 if cntbrthc_num==75 /*Guyana*/
replace codecow2=997 if cntbrthc_num==76 /*Hong Kong*/
replace codecow2=91 if cntbrthc_num==77 /*Honduras*/
replace codecow2=344 if cntbrthc_num==78 /*Croatia*/
replace codecow2=310 if cntbrthc_num==79 /*Hungary*/
replace codecow2=850 if cntbrthc_num==80 /*Indonesia*/
replace codecow2=205 if cntbrthc_num==81 /*Ireland*/
replace codecow2=666 if cntbrthc_num==82 /*Israel*/
replace codecow2=750 if cntbrthc_num==83 /*India*/
replace codecow2=645 if cntbrthc_num==84 /*Iraq*/
replace codecow2=630 if cntbrthc_num==85 /*Iran*/
replace codecow2=395 if cntbrthc_num==86 /*Iceland*/
replace codecow2=325 if cntbrthc_num==87 /*Italy*/
replace codecow2=51 if cntbrthc_num==89 /*Jamaica*/
replace codecow2=663 if cntbrthc_num==90 /*Jordan*/
replace codecow2=740 if cntbrthc_num==91 /*Japan*/
replace codecow2=501 if cntbrthc_num==92 /*Kenya*/
replace codecow2=703 if cntbrthc_num==93 /*Kyrgyzstan*/
replace codecow2=811 if cntbrthc_num==94 /*Cambodia*/
replace codecow2=581 if cntbrthc_num==95 /*Comoros*/
replace codecow2=732 if cntbrthc_num==96 /*Korea, Republic of*/
replace codecow2=690 if cntbrthc_num==97 /*Kuwait*/
replace codecow2=705 if cntbrthc_num==98 /*Kazakhstan*/
replace codecow2=812 if cntbrthc_num==99 /*Lao People's Democratic Republic*/
replace codecow2=660 if cntbrthc_num==100 /*Lebanon*/
replace codecow2=56 if cntbrthc_num==101 /*Saint Lucia*/
replace codecow2=780 if cntbrthc_num==102 /*Sri Lanka*/
replace codecow2=450 if cntbrthc_num==103 /*Liberia*/
replace codecow2=570 if cntbrthc_num==104 /*Lesotho*/
replace codecow2=368 if cntbrthc_num==105 /*Lithuania*/
replace codecow2=212 if cntbrthc_num==106 /*Luxembourg*/
replace codecow2=367 if cntbrthc_num==107 /*Latvia*/
replace codecow2=620 if cntbrthc_num==108 /*Lybia*/
replace codecow2=600 if cntbrthc_num==109 /*Morocco*/
replace codecow2=359 if cntbrthc_num==110 /*Moldova, Republic of*/
replace codecow2=580 if cntbrthc_num==111 /*Madagascar*/
replace codecow2=343 if cntbrthc_num==112 /*Macedonia, The Former Yugoslav Republic of*/
replace codecow2=432 if cntbrthc_num==113 /*Mali*/
replace codecow2=712 if cntbrthc_num==114 /*Mongolia*/
replace codecow2=435 if cntbrthc_num==116 /*Mauritania*/
replace codecow2=338 if cntbrthc_num==117 /*Malta*/
replace codecow2=590 if cntbrthc_num==118 /*Mauritius*/
replace codecow2=553 if cntbrthc_num==119 /*Malawi*/
replace codecow2=70 if cntbrthc_num==120 /*Mexico*/
replace codecow2=820 if cntbrthc_num==121 /*Malaysia*/
replace codecow2=541 if cntbrthc_num==122 /*Mozambique*/
replace codecow2=436 if cntbrthc_num==123 /*Niger*/
replace codecow2=475 if cntbrthc_num==124 /*Nigeria*/
replace codecow2=93 if cntbrthc_num==125 /*Nicaragua*/
replace codecow2=210 if cntbrthc_num==126 /*Netherlands*/
replace codecow2=385 if cntbrthc_num==127 /*Norway*/
replace codecow2=790 if cntbrthc_num==128 /*Nepal*/
replace codecow2=920 if cntbrthc_num==129 /*New Zealand*/
replace codecow2=95 if cntbrthc_num==130 /*Panama*/
replace codecow2=135 if cntbrthc_num==131 /*Peru*/
replace codecow2=840 if cntbrthc_num==133 /*Philipnnes*/
replace codecow2=770 if cntbrthc_num==134 /*Pakistan*/
replace codecow2=290 if cntbrthc_num==135 /*Poland*/
replace codecow2=666 if cntbrthc_num==137 /*Palestine (israel)*/
replace codecow2=235 if cntbrthc_num==138 /*Portugal*/
replace codecow2=150 if cntbrthc_num==139 /*Paraguay*/
replace codecow2=360 if cntbrthc_num==141 /*Romania*/
replace codecow2=345 if cntbrthc_num==142 /*Serbia*/
replace codecow2=365 if cntbrthc_num==143 /*Russia*/
replace codecow2=517 if cntbrthc_num==144 /*Rwanda*/
replace codecow2=670 if cntbrthc_num==145 /*Saudi Arabia*/
replace codecow2=591 if cntbrthc_num==146 /*Seychelles*/
replace codecow2=625 if cntbrthc_num==147 /*Sudan*/
replace codecow2=380 if cntbrthc_num==148 /*Sweden*/
replace codecow2=830 if cntbrthc_num==149 /*Singapore*/
replace codecow2=349 if cntbrthc_num==150 /*Slovenia*/
replace codecow2=317 if cntbrthc_num==151 /*Slovakia*/
replace codecow2=451 if cntbrthc_num==152 /*Sierra Leone*/
replace codecow2=433 if cntbrthc_num==153 /*Senegal*/
replace codecow2=520 if cntbrthc_num==154 /*Somalia*/
replace codecow2=115 if cntbrthc_num==155 /*Suriname*/
replace codecow2=403 if cntbrthc_num==156 /*Sao Tome and Principe*/
replace codecow2=92 if cntbrthc_num==157 /*El Salvador*/
replace codecow2=652 if cntbrthc_num==159 /*Syrian*/
replace codecow2=483 if cntbrthc_num==160 /*Chad*/
replace codecow2=461 if cntbrthc_num==161 /*Togo*/
replace codecow2=800 if cntbrthc_num==162 /*Thailand*/
replace codecow2=702 if cntbrthc_num==163 /*Tajikistan*/
replace codecow2=701 if cntbrthc_num==164 /*Turkmenistan*/
replace codecow2=616 if cntbrthc_num==165 /*Tunisia*/
replace codecow2=640 if cntbrthc_num==166 /*Turkey*/
replace codecow2=52 if cntbrthc_num==167 /*Trinidad and Tobago*/
replace codecow2=713 if cntbrthc_num==168 /*Taiwan*/
replace codecow2=510 if cntbrthc_num==169 /*Tanzania*/
replace codecow2=369 if cntbrthc_num==170 /*Ukraine*/
replace codecow2=500 if cntbrthc_num==171 /*Uganda*/
replace codecow2=2 if cntbrthc_num==172 /*US*/
replace codecow2=165 if cntbrthc_num==173 /*Uruguay*/
replace codecow2=704 if cntbrthc_num==174 /*Uzbekistan*/
replace codecow2=101 if cntbrthc_num==175 /*Venezuela*/
replace codecow2=816 if cntbrthc_num==176 /*Vietnam*/
replace codecow2=347 if cntbrthc_num==177 /*Kosovo*/
replace codecow2=560 if cntbrthc_num==179 /*South Africa*/
replace codecow2=551 if cntbrthc_num==180 /*Zambia*/
replace codecow2=552 if cntbrthc_num==181 /*Zimbabwe*/


capture drop _merge
merge m:m codecow2 year using qog_std_ts_jan20.dta,  keepusing(wdi_gdppppcon2011 wdi_unempilo wdi_chexppgdp wdi_expedu vdem_libdem )

gen gdp_ratio=gdp/wdi_gdppppcon2011
gen unemploy_ratio=unemp_rate/wdi_unempilo
gen healthexpend_ratio=health_expend/wdi_chexppgdp
gen educexpend_ratio=education_expend/wdi_expedu


gen female=gndr==2
gen age2=agea^2





gen length_stay=inwyys-year /*length of stay*/
replace length_stay=inwyr-year if essround==1
replace length_stay=inwyr-year if essround==2

drop if length_stay<0

sum length_stay, d
gen length_stay_rec=(length_stay-r(min))/(r(max)-r(min))

gen age_migr=year-yrbrn /*age when migrating*/
*drop if age_migr <18



egen country_year=group(country essround), label


capture drop external_cat
xtile external_cat=AvgS_ExtReg_B, nq(10)
xtile internal_cat=AvgS_IntReg_B, nq(10)
gen interaction=external_cat*internal_cat

gen external = AvgS_ExtReg_A+AvgS_ExtReg_B+AvgS_ExtReg_C

gen internal = AvgS_IntReg_A+AvgS_IntReg_B+AvgS_IntReg_C

recode brncntr 2=1 1=0, gen(migrant)

label define migr 1"Migrant" 0"Native"
label val migrant migr

gen origin=.
replace origin=1 if migrant==0
replace origin=2 if eu==1
replace origin=3 if eu==0

label define origin 1"Native" 2"EU" 3"Non-EU"
label val origin origin

gen west=0
replace west=1 if country==1
replace west=1 if country==2
replace west=1 if country==4
replace west=1 if country==7
replace west=1 if country==8
replace west=1 if country==10
replace west=1 if country==11
replace west=1 if country==12
replace west=1 if country==13
replace west=1 if country==14
replace west=1 if country==17
replace west=1 if country==19
replace west=1 if country==20
replace west=1 if country==22
replace west=1 if country==23
replace west=1 if country==24
replace west=1 if country==26
replace west=1 if country==28



save ESS_IMPIC_migrants.dta, replace


set scheme gg_tableau

*Contemporaneous (living through) policy regimes




*Contemporaneous migrants vs natives

mixed stfdem c.external##c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3) migrant=(0 1))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

 graph export "$pathfig/ess_satdem_natives_contemp.pdf", as(pdf) replace

 
 mixed trust_politicians c.external##c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2  || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3) migrant=(0 1))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_poltrust_natives_contemp.pdf", as(pdf) replace

mixed trust_legal c.external##c.internal##migrant eduyrs female agea age2 expenditure unemp_rate epl i.country if origin !=2 || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3) migrant=(0 1))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0, lc(black)) recastci(rspike)

graph export "$pathfig/ess_legaltrust_natives_contemp.pdf", as(pdf) replace


*Contemporaneous migrants vs migrants

mixed stfdem c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 


margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(I, replace) recastci(rspike) yline(0, lc(black)) 

 graph export "$pathfig/ess_satdem_migrants_contemp.pdf", as(pdf) replace

 mixed trust_politicians c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(I, replace) recastci(rspike) yline(0, lc(black))

///
*addplot(scatter where internal, mcolor(none) mlabel(pipe) mlabc(black) ylabel(-2(1)6) xlabel(0.5(0.5)3) legend(off) below)

 graph export "$pathfig/ess_poltrust_migrants_contemp.pdf", as(pdf) replace
 
 mixed trust_legal c.external##c.internal eduyrs female agea age2 expenditure unemp_rate epl i.country if migrant==1 & eu==0 || country_year:, mle variance 

margins, dydx(c.external) at(internal=(0.5(0.5)3))
marginsplot, title("") ytitle("Effect migrant selectivity", size(6))  ///
xtitle("Strictness internal regulations", size(6)) yline(0) name(I, replace) recastci(rspike) yline(0, lc(black)) 

///
*addplot(scatter where internal, mcolor(none) mlabel(pipe) mlabc(black) ylabel(-2(1)4) xlabel(0.5(0.5)3) legend(off) below)

 graph export "$pathfig/ess_trustlegal_migrants_contemp.pdf", as(pdf) replace
